-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_test.go
88 lines (82 loc) · 3.14 KB
/
client_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package stopandshop
import (
"testing"
"github.com/blaskovicz/go-stopandshop/mocks"
"github.com/blaskovicz/go-stopandshop/models"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestClient(t *testing.T) {
s := mocks.NewStopAndShopAPI()
defer s.Close()
username := "testuser"
password := "testpass"
var err error
var client *Client
var profile *models.Profile
t.Run("Init", func(t *testing.T) {
client = New()
client.SetRootURI(s.URL())
require.NotNil(t, client, "client was nil")
})
t.Run("Login", func(t *testing.T) {
err = client.Login(username, password)
require.NoError(t, err, "login failed")
require.NotNil(t, client.Token())
require.NotEmpty(t, client.Token().AccessToken)
require.NotNil(t, client.Token().RefreshToken)
require.NotEmpty(t, *(client.Token().RefreshToken))
require.NotNil(t, client.Token().ExpiresIn)
})
t.Run("ReadProfile", func(t *testing.T) {
require.NotNil(t, client)
profile, err = client.ReadProfile()
require.NoError(t, err, "read profile failed")
require.NotNil(t, profile, "profile was nil")
require.Equal(t, "test", profile.FirstName)
require.Equal(t, "12345", profile.CardNumber)
require.Equal(t, "[email protected]", profile.Login)
})
t.Run("ReadCoupons", func(t *testing.T) {
require.NotNil(t, client)
require.NotNil(t, profile)
var cs []models.Coupon
cs, err = client.ReadCoupons(profile.CardNumber)
require.NoError(t, err, "read coupons failed")
require.NotNil(t, cs, "coupons were nil")
require.Equal(t, 2, len(cs))
c := cs[0]
assert.Equal(t, "0a06f213-298d-47cc-9260-99fc4450c0a4", c.ID, "id mismatch")
assert.Equal(t, "Fortify™", c.Name, "name mistmatch")
assert.Equal(t, "On any Fortify™ 50 Billion Formula", c.Description, "description mismatch")
assert.False(t, c.Loaded, "coupon loaded")
c = cs[1]
assert.Equal(t, "2b9f19ca-7655-499a-8284-491c039068b4", c.ID, "id mismatch")
assert.Equal(t, "Duncan Hines®", c.Name, "name mistmatch")
assert.Equal(t, "Duncan Hines® Perfect Size for 1® Cake Mix (Max. Value $3.49)", c.Description, "description mismatch")
assert.True(t, c.Loaded, "coupon not loaded")
})
t.Run("LoadCouponToProfile", func(t *testing.T) {
require.NotNil(t, client)
require.NotNil(t, profile)
err := client.LoadCoupon(profile.CardNumber, "0a06f213-298d-47cc-9260-99fc4450c0a4")
require.NoError(t, err, "load coupon failed")
err = client.LoadCoupon(profile.CardNumber, "0a06f213-298d-47cc-9260-99fc4450c0a4")
require.NoError(t, err, "load coupon failed")
err = client.LoadCoupon(profile.CardNumber, "")
require.Error(t, err, "load coupon failed")
})
t.Run("RefreshAccessToken", func(t *testing.T) {
require.NotNil(t, client)
t1 := client.Token()
err := client.RefreshAccessToken()
require.NoError(t, err, "refresh failed")
require.NotNil(t, client.Token())
require.NotEmpty(t, client.Token().AccessToken)
require.NotEqual(t, t1.AccessToken, client.Token().AccessToken)
require.NotNil(t, client.Token().RefreshToken)
require.NotEmpty(t, *(client.Token().RefreshToken))
require.NotEqual(t, *t1.RefreshToken, *(client.Token().RefreshToken))
require.NotNil(t, client.Token().ExpiresIn)
})
}