-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathauthentication_test.go
120 lines (95 loc) · 3.28 KB
/
authentication_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
package flickr
import (
"testing"
flickErr "gopkg.in/masci/flickr.v3/error"
)
func TestParseRequestToken(t *testing.T) {
in := "oauth_callback_confirmed=true&oauth_token=72157654304937659-8eedcda57d9d57e3&oauth_token_secret=8700d234e3fc00c6"
expected := RequestToken{true, "72157654304937659-8eedcda57d9d57e3", "8700d234e3fc00c6", ""}
tok, err := ParseRequestToken(in)
Expect(t, nil, err)
Expect(t, *tok, expected)
}
func TestParseRequestTokenKo(t *testing.T) {
in := "oauth_problem=foo"
tok, err := ParseRequestToken(in)
ee, ok := err.(*flickErr.Error)
if !ok {
t.Error("err is not a flickErr.Error!")
}
Expect(t, ee.ErrorCode, 20)
Expect(t, tok.OAuthProblem, "foo")
tok, err = ParseRequestToken("notA%%%ValidUrl")
if err == nil {
t.Error("Parsing an invalid URL string should rise an error")
}
}
func TestGetRequestToken(t *testing.T) {
fclient := GetTestClient()
mocked_body := "oauth_callback_confirmed=true&oauth_token=72157654304937659-8eedcda57d9d57e3&oauth_token_secret=8700d234e3fc00c6"
server, client := FlickrMock(200, mocked_body, "")
defer server.Close()
// use the mocked client
fclient.HTTPClient = client
tok, err := GetRequestToken(fclient)
if err != nil {
t.Error("Unexpected error:", err)
}
Expect(t, tok.OauthCallbackConfirmed, true)
Expect(t, tok.OauthToken, "72157654304937659-8eedcda57d9d57e3")
Expect(t, tok.OauthTokenSecret, "8700d234e3fc00c6")
}
func TestGetAuthorizeUrl(t *testing.T) {
client := GetTestClient()
tok := &RequestToken{true, "token", "token_secret", ""}
url, err := GetAuthorizeUrl(client, tok)
Expect(t, err, nil)
Expect(t, url, "https://www.flickr.com/services/oauth/authorize?oauth_token=token&perms=delete")
}
func TestParseOAuthToken(t *testing.T) {
response := "fullname=Jamal%20Fanaian" +
"&oauth_token=72157626318069415-087bfc7b5816092c" +
"&oauth_token_secret=a202d1f853ec69de" +
"&user_nsid=21207597%40N07" +
"&username=jamalfanaian"
tok, _ := ParseOAuthToken(response)
Expect(t, tok.OAuthToken, "72157626318069415-087bfc7b5816092c")
Expect(t, tok.OAuthTokenSecret, "a202d1f853ec69de")
Expect(t, tok.UserNsid, "21207597@N07")
Expect(t, tok.Username, "jamalfanaian")
Expect(t, tok.Fullname, "Jamal Fanaian")
}
func TestParseOAuthTokenKo(t *testing.T) {
response := "oauth_problem=foo"
tok, err := ParseOAuthToken(response)
ee, ok := err.(*flickErr.Error)
if !ok {
t.Error("err is not a flickErr.Error!")
}
Expect(t, ee.ErrorCode, 30)
Expect(t, tok.OAuthProblem, "foo")
tok, err = ParseOAuthToken("notA%%%ValidUrl")
if err == nil {
t.Error("Parsing an invalid URL string should rise an error")
}
}
func TestGetAccessToken(t *testing.T) {
body := "fullname=Jamal%20Fanaian" +
"&oauth_token=72157626318069415-087bfc7b5816092c" +
"&oauth_token_secret=a202d1f853ec69de" +
"&user_nsid=21207597%40N07" +
"&username=jamalfanaian"
fclient := GetTestClient()
server, client := FlickrMock(200, body, "")
defer server.Close()
// use the mocked client
fclient.HTTPClient = client
rt := &RequestToken{true, "token", "token_secret", ""}
_, err := GetAccessToken(fclient, rt, "fooVerifier")
if err != nil {
t.Error("Unexpected error:", err)
}
Expect(t, fclient.Id, "21207597@N07")
Expect(t, fclient.OAuthToken, "72157626318069415-087bfc7b5816092c")
Expect(t, fclient.OAuthTokenSecret, "a202d1f853ec69de")
}