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_identity_test.go
87 lines (75 loc) · 2.12 KB
/
user_identity_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
package moneybutton
import (
"bytes"
"context"
"fmt"
"io/ioutil"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
)
// mockHTTPGetUserIdentity for mocking requests
type mockHTTPGetUserIdentity struct{}
// Do is a mock http request
func (m *mockHTTPGetUserIdentity) 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() == endpointUserIdentity {
resp.StatusCode = http.StatusOK
resp.Body = ioutil.NopCloser(bytes.NewBuffer([]byte(`{"data":{"id":"123","type":"user_identities","attributes":{"id":"123","name":"MrZ"}},"jsonapi":{"version":"1.0"}}`)))
}
// Default is valid
return resp, nil
}
func TestClient_GetUserIdentity(t *testing.T) {
t.Parallel()
t.Run("missing access token", func(t *testing.T) {
client := newTestClient(&mockHTTPGetUserIdentity{})
assert.NotNil(t, client)
identity, err := client.GetUserIdentity(
context.Background(),
"",
)
assert.Error(t, err)
assert.Nil(t, identity)
})
t.Run("api error response", func(t *testing.T) {
client := newTestClient(&mockHTTPAPIError{})
assert.NotNil(t, client)
identity, err := client.GetUserIdentity(
context.Background(),
"1234567",
)
assert.Error(t, err)
assert.Nil(t, identity)
})
t.Run("http error", func(t *testing.T) {
client := newTestClient(&mockHTTPError{})
assert.NotNil(t, client)
identity, err := client.GetUserIdentity(
context.Background(),
"1234567",
)
assert.Error(t, err)
assert.Nil(t, identity)
})
t.Run("valid response", func(t *testing.T) {
client := newTestClient(&mockHTTPGetUserIdentity{})
assert.NotNil(t, client)
identity, err := client.GetUserIdentity(
context.Background(),
"1234567",
)
assert.NoError(t, err)
assert.NotNil(t, identity)
assert.Equal(t, "1.0", identity.JSONAPI.Version)
assert.Equal(t, "user_identities", identity.Data.Type)
assert.Equal(t, "123", identity.Data.ID)
assert.Equal(t, "123", identity.Data.Attributes.ID)
assert.Equal(t, "MrZ", identity.Data.Attributes.Name)
})
}