-
Notifications
You must be signed in to change notification settings - Fork 5
/
token_test.go
82 lines (73 loc) · 2.83 KB
/
token_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
package wallabago
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
)
func TestGetToken(t *testing.T) {
mux := http.NewServeMux()
server := httptest.NewServer(mux)
defer server.Close()
wbgcfg := NewWallabagConfig(server.URL, "asdf", "ghkj", "wallabago", "555nase")
SetConfig(wbgcfg)
mux.HandleFunc("/oauth/v2/token", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
w.Header().Set("Content-Type", "application/json")
fmt.Fprintln(w, `{"access_token":"294hf92ufurjfgoiqjfioj4","refresh_token": "ZGE5MDg3ZTNjNmNkYTY0ZWZh","expires_in":3600,"scope": "null", "token_type": "bearer"}`)
})
token, err := getToken()
if err != nil {
t.Errorf("expected no error, but got %v", err)
}
expectedToken := tokenResponse{"294hf92ufurjfgoiqjfioj4", 3600, "bearer", "null", "ZGE5MDg3ZTNjNmNkYTY0ZWZh"}
if *token != expectedToken {
t.Errorf("TestGetToken(): expected %v, got %v", expectedToken, token)
}
}
func TestRefreshToken(t *testing.T) {
mux := http.NewServeMux()
server := httptest.NewServer(mux)
defer server.Close()
wbgcfg := NewWallabagConfig(server.URL, "asdf", "ghkj", "wallabago", "555nase")
SetConfig(wbgcfg)
mux.HandleFunc("/oauth/v2/token", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
w.Header().Set("Content-Type", "application/json")
fmt.Fprintln(w, `{"access_token":"294hf92ufurjfgoiqjfioj4","refresh_token": "ZGE5MDg3ZTNjNmNkYTY0ZWZh","expires_in":3600,"scope": "null", "token_type": "bearer"}`)
})
// Set the token first so we can refresh it later
token, err := getToken()
setToken(responseToToken(token))
if err != nil {
t.Errorf("expected no error, but got %v", err)
}
refreshedToken, err := refreshToken()
if err != nil {
t.Errorf("expected no error, but got %v", err)
}
expectedToken := tokenResponse{"294hf92ufurjfgoiqjfioj4", 3600, "bearer", "null", "ZGE5MDg3ZTNjNmNkYTY0ZWZh"}
if *refreshedToken != expectedToken {
t.Errorf("TestGetToken(): expected %v, got %v", expectedToken, token)
}
}
func TestGetAuthTokenHeader(t *testing.T) {
mux := http.NewServeMux()
server := httptest.NewServer(mux)
defer server.Close()
wbgcfg := NewWallabagConfig(server.URL, "asdf", "ghkj", "wallabago", "555nase")
SetConfig(wbgcfg)
mux.HandleFunc("/oauth/v2/token", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
w.Header().Set("Content-Type", "application/json")
fmt.Fprintln(w, `{"access_token":"294hf92ufurjfgoiqjfioj4","refresh_token": "ZGE5MDg3ZTNjNmNkYTY0ZWZh","expires_in":3600,"scope": "null", "token_type": "bearer"}`)
})
authTokenHeader, err := GetAuthTokenHeader()
if err != nil {
t.Errorf("expected no error, but got %v", err)
}
expectedAuthTokenHeader := "Bearer 294hf92ufurjfgoiqjfioj4"
if authTokenHeader != expectedAuthTokenHeader {
t.Errorf("TestGetAuthTokenHeader(): expected %v, got %v", expectedAuthTokenHeader, authTokenHeader)
}
}