This repository has been archived by the owner on Aug 14, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuser_profile_test.go
119 lines (105 loc) · 3.23 KB
/
user_profile_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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package moneybutton
import (
"bytes"
"context"
"fmt"
"io/ioutil"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
)
// mockHTTPGetUserProfile for mocking requests
type mockHTTPGetUserProfile struct{}
// Do is a mock http request
func (m *mockHTTPGetUserProfile) Do(req *http.Request) (*http.Response, error) {
resp := new(http.Response)
resp.StatusCode = http.StatusBadRequest
// No req found
if req == nil {
return resp, fmt.Errorf("missing request")
}
if req.URL.String() == fmt.Sprintf(endpointUserProfile, "123") {
resp.StatusCode = http.StatusOK
resp.Body = ioutil.NopCloser(bytes.NewBuffer([]byte(`{"data":{"type":"profiles","id":"123","attributes":{"created-at":"2019-03-26T17:33:42.788Z","name":"MrZ","default-currency":"USD","default-language":"en","bio":"I like Money Button.","primary-paymail":"[email protected]","avatar-url":"https://www.gravatar.com/avatar/372bc0ab9b8a8930d4a86b2c5b11f11e?d=identicon"}}}`)))
}
// Default is valid
return resp, nil
}
func TestClient_GetProfile(t *testing.T) {
t.Parallel()
t.Run("missing all parameters", func(t *testing.T) {
client := newTestClient(&mockHTTPGetUserProfile{})
assert.NotNil(t, client)
profile, err := client.GetProfile(
context.Background(),
"",
"",
)
assert.Error(t, err)
assert.Nil(t, profile)
})
t.Run("missing user id", func(t *testing.T) {
client := newTestClient(&mockHTTPGetUserProfile{})
assert.NotNil(t, client)
profile, err := client.GetProfile(
context.Background(),
"",
"1234567",
)
assert.Error(t, err)
assert.Nil(t, profile)
})
t.Run("missing access token", func(t *testing.T) {
client := newTestClient(&mockHTTPGetUserProfile{})
assert.NotNil(t, client)
profile, err := client.GetProfile(
context.Background(),
"123",
"",
)
assert.Error(t, err)
assert.Nil(t, profile)
})
t.Run("api error response", func(t *testing.T) {
client := newTestClient(&mockHTTPAPIError{})
assert.NotNil(t, client)
profile, err := client.GetProfile(
context.Background(),
"123",
"1234567",
)
assert.Error(t, err)
assert.Nil(t, profile)
})
t.Run("http error", func(t *testing.T) {
client := newTestClient(&mockHTTPError{})
assert.NotNil(t, client)
profile, err := client.GetProfile(
context.Background(),
"123",
"1234567",
)
assert.Error(t, err)
assert.Nil(t, profile)
})
t.Run("valid response", func(t *testing.T) {
client := newTestClient(&mockHTTPGetUserProfile{})
assert.NotNil(t, client)
profile, err := client.GetProfile(
context.Background(),
"123",
"1234567",
)
assert.NoError(t, err)
assert.NotNil(t, profile)
assert.Equal(t, "profiles", profile.Data.Type)
assert.Equal(t, "123", profile.Data.ID)
assert.Equal(t, "MrZ", profile.Data.Attributes.Name)
assert.Equal(t, "https://www.gravatar.com/avatar/372bc0ab9b8a8930d4a86b2c5b11f11e?d=identicon", profile.Data.Attributes.AvatarURL)
assert.Equal(t, "I like Money Button.", profile.Data.Attributes.Bio)
assert.Equal(t, "2019-03-26T17:33:42.788Z", profile.Data.Attributes.CreatedAt)
assert.Equal(t, "USD", profile.Data.Attributes.DefaultCurrency)
assert.Equal(t, "en", profile.Data.Attributes.DefaultLanguage)
assert.Equal(t, "[email protected]", profile.Data.Attributes.PrimaryPaymail)
})
}