forked from mrjones/oauth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
provider_test.go
72 lines (56 loc) · 2.58 KB
/
provider_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
package oauth
import (
"net/http"
"testing"
)
func TestProviderIsAuthorizedGood(t *testing.T) {
p := NewProvider(func(s string, h map[string]string) (*Consumer, error) {
c := NewConsumer(s, "consumersecret", ServiceProvider{})
c.signer = &MockSigner{}
return c, nil
})
p.clock = &MockClock{Time: 1446226936}
fakeRequest, err := http.NewRequest("GET", "https://example.com/some/path?q=query&q1=another_query", nil)
if err != nil {
t.Fatal(err)
}
// Set header to good oauth1 header
fakeRequest.Header.Set(HTTP_AUTH_HEADER, "OAuth oauth_nonce=\"799507437267152061446226936\", oauth_timestamp=\"1446226936\", oauth_version=\"1.0\", oauth_signature_method=\"HMAC-SHA1\", oauth_consumer_key=\"consumerkey\", oauth_signature=\"MOCK_SIGNATURE\"")
authorized, err := p.IsAuthorized(fakeRequest)
assertEq(t, nil, err)
assertEq(t, "consumerkey", *authorized)
}
func TestProviderIsAuthorizedWithBodyHash(t *testing.T) {
p := NewProvider(func(s string, h map[string]string) (*Consumer, error) {
c := NewConsumer(s, "consumersecret", ServiceProvider{BodyHash: true})
c.Debug(true)
return c, nil
})
p.clock = &MockClock{Time: 1446226936}
fakeRequest, err := http.NewRequest("GET", "https://example.com/some/path?q=query&q1=another_query", nil)
if err != nil {
t.Fatal(err)
}
// Set header to good oauth1 header
fakeRequest.Header.Set(HTTP_AUTH_HEADER, "OAuth oauth_nonce=\"799507437267152061446226936\", oauth_timestamp=\"1446226936\", oauth_version=\"1.0\", oauth_signature_method=\"HMAC-SHA1\", oauth_consumer_key=\"consumerkey\", oauth_signature=\"RYUiwiUc5LHoipANhDxPbdFHgKc%3D\", oauth_body_hash=\"2jmj7l5rSw0yVb%2FvlWAYkK%2FYBwk%3D\"")
authorized, err := p.IsAuthorized(fakeRequest)
assertEq(t, nil, err)
assertEq(t, "consumerkey", *authorized)
}
func TestConsumerKeyWithEqualsInIt(t *testing.T) {
p := NewProvider(func(s string, h map[string]string) (*Consumer, error) {
c := NewConsumer(s, "consumersecret", ServiceProvider{})
c.signer = &MockSigner{}
return c, nil
})
p.clock = &MockClock{Time: 1446226936}
fakeRequest, err := http.NewRequest("GET", "https://example.com/some/path?q=query&q1=another_query", nil)
if err != nil {
t.Fatal(err)
}
// Set header to good oauth1 header
fakeRequest.Header.Set(HTTP_AUTH_HEADER, "OAuth oauth_nonce=\"799507437267152061446226936\", oauth_timestamp=\"1446226936\", oauth_version=\"1.0\", oauth_signature_method=\"HMAC-SHA1\", oauth_consumer_key=\"consumerkeywithequals=\", oauth_signature=\"MOCK_SIGNATURE\"")
authorized, err := p.IsAuthorized(fakeRequest)
assertEq(t, nil, err)
assertEq(t, "consumerkeywithequals=", *authorized)
}