-
Notifications
You must be signed in to change notification settings - Fork 0
/
ApiClient_test.go
91 lines (78 loc) · 2.03 KB
/
ApiClient_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
package kneu
import (
"fmt"
"github.com/h2non/gock"
"github.com/stretchr/testify/assert"
"testing"
)
func TestApiClient_GetUserMe(t *testing.T) {
t.Run("success", func(t *testing.T) {
expectedUserMe := UserMeResponse{
Id: 999,
Email: "[email protected]",
Name: "Петренко Петр Петрович",
LastName: "Петренко",
FirstName: "Петр",
MiddleName: "Петрович",
Type: "student",
StudentId: 123,
GroupId: 50,
Gender: "male",
TeacherId: 0,
DepartmentId: 0,
}
client := ApiClient{
AccessToken: "test-access-token",
}
gock.New(AuthBaseUri).
Get("/api/user/me").
MatchHeader("Authorization", "Bearer test-access-token").
Reply(200).
JSON(`{
"id": 999,
"email": "[email protected]",
"name": "Петренко Петр Петрович",
"last_name": "Петренко",
"first_name": "Петр",
"middle_name": "Петрович",
"type": "student",
"student_id": 123,
"group_id": 50,
"gender": "male"
}`)
userMe, err := client.GetUserMe()
assert.NoError(t, err)
assert.Equal(t, expectedUserMe, userMe)
})
t.Run("http_error", func(t *testing.T) {
client := ApiClient{
AccessToken: "test-access-token",
}
gock.New(AuthBaseUri).
Get("/api/user/me").
MatchHeader("Authorization", "Bearer test-access-token").
Reply(500)
userMe, err := client.GetUserMe()
fmt.Println(err.Error())
assert.Error(t, err)
assert.Equal(t, "Receive http code: 500", err.Error())
assert.Empty(t, userMe)
})
t.Run("api_error", func(t *testing.T) {
client := ApiClient{
AccessToken: "test-access-token",
}
gock.New(AuthBaseUri).
Get("/api/user/me").
MatchHeader("Authorization", "Bearer test-access-token").
Reply(402).
JSON(`{
"error": "Test error description"
}`)
userMe, err := client.GetUserMe()
fmt.Println(err.Error())
assert.Error(t, err)
assert.Equal(t, "API error: Test error description", err.Error())
assert.Empty(t, userMe)
})
}