-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsession_test.go
148 lines (107 loc) · 3.2 KB
/
session_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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package clarifai
import (
"net/http"
"reflect"
"testing"
)
func TestNewSession(t *testing.T) {
sess := NewSession(mockClientID, mockClientSecret)
actual := reflect.TypeOf(sess).String()
expected := "*clarifai.Session"
if actual != expected {
t.Fatalf("Actual: %v, expected: %v", actual, expected)
}
if sess.clientID != mockClientID {
t.Errorf("Actual: %v, expected: %v", sess.clientID, mockClientID)
}
if sess.clientSecret != mockClientSecret {
t.Errorf("Actual: %v, expected: %v", sess.clientSecret, mockClientSecret)
}
if sess.accessToken != "" {
t.Errorf("Actual: %v, expected: %v", sess.accessToken, "to be empty")
}
}
func TestBuildURI(t *testing.T) {
sess := NewSession(mockClientID, mockClientSecret)
actual := sess.buildURI("foo")
expected := apiHost + "/" + apiVersion + "/" + "foo"
if actual != expected {
t.Errorf("Actual: %v, expected: %v", actual, expected)
}
}
func TestAuthResponseValidation_Success(t *testing.T) {
resp := &AuthResponse{
AccessToken: "foo",
}
err := authResponseValidation(resp)
if err != nil {
t.Errorf("Should return no error, but got %v", err)
}
}
func TestAuthResponseValidation_Fail_No_Token(t *testing.T) {
actual := authResponseValidation(&AuthResponse{})
expected := ErrNoAuthenticationToken
if actual != expected {
t.Errorf("Actual: %v, expected: %v", actual, expected)
}
}
func TestAuthResponseValidation_SetTokenExpiration(t *testing.T) {
expiresIn := 100
sess := NewSession(mockClientID, mockClientSecret)
resp := &AuthResponse{
ExpiresIn: expiresIn,
}
startingTime, expirationTime := sess.setTokenExpiration(resp.ExpiresIn)
if expirationTime != startingTime+expiresIn {
t.Errorf("Actual: %v, expected: %v", expirationTime, startingTime+expiresIn)
}
}
func TestAuthResponseValidation_IsTokenExpired_False(t *testing.T) {
expiresIn := 100
sess := NewSession(mockClientID, mockClientSecret)
resp := &AuthResponse{
ExpiresIn: expiresIn,
}
sess.setTokenExpiration(resp.ExpiresIn)
actual := sess.isTokenExpired()
expected := false
if actual != expected {
t.Errorf("Actual: %v, expected: %v", actual, expected)
}
}
func TestAuthResponseValidation_IsTokenExpired_True(t *testing.T) {
sess := NewSession(mockClientID, mockClientSecret)
sess.tokenExpiration = 0
actual := sess.isTokenExpired()
expected := true
if actual != expected {
t.Errorf("Actual: %v, expected: %v", actual, expected)
}
}
func TestSession_Connect(t *testing.T) {
mockRoute(t, "token", "resp/ok_auth.json")
err := sess.Connect()
if err != nil {
t.Fatalf("Should have no errors, but got %v", err)
}
actual := sess.accessToken
expected := "bCGdwie3gIJoRISG5Ejz2Je57inNTj"
if expected != actual {
t.Errorf("Actual: %v, expected: %v", actual, expected)
}
}
func TestApiKeyAuth(t *testing.T) {
mux.HandleFunc("/"+apiVersion+"/key-test", func(w http.ResponseWriter, r *http.Request) {
actual := r.Header.Get("Authorization")
expected := "Key test_api_key"
if expected != actual {
t.Errorf("Actual: %v, expected: %v", actual, expected)
}
w.WriteHeader(200)
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(`{"ok":true}`))
})
app := NewApp("test_api_key")
app.host = ts.URL
app.HTTPCall("GET", "key-test", nil)
}