forked from gophergala/authy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
authy_test.go
83 lines (68 loc) · 2.24 KB
/
authy_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
package authy_test
import (
"github.com/christopherobin/authy"
"github.com/christopherobin/authy/provider"
. "github.com/smartystreets/goconvey/convey"
"net/url"
"testing"
)
var config = authy.Config{
PathLogin: "/login",
Callback: "/login/success",
Providers: map[string]provider.ProviderConfig{
"github": provider.ProviderConfig{
Key: "my-key",
Secret: "my-secret",
Scope: []string{"repo", "user:mail"},
},
},
}
var badConfig = authy.Config{
Providers: map[string]provider.ProviderConfig{
"invalid": provider.ProviderConfig{
Key: "my-key",
},
},
}
func TestAuthy(t *testing.T) {
Convey("Invalid configuration", t, func() {
_, err := authy.NewAuthy(badConfig)
So(err, ShouldNotEqual, nil)
})
Convey("Instanciate Authy", t, func() {
a, err := authy.NewAuthy(config)
So(err, ShouldEqual, nil)
// create a fake session
session := &FakeSession{
items: map[interface{}]interface{}{},
}
// and a fake oauth
server := MockOAuthServer(t)
// and even a fake github
github, _ := provider.GetProvider("github")
github.AuthorizeURL = server.URL + "/oauth2"
github.AccessURL = server.URL + "/oauth2"
provider.RegisterProvider(github)
Convey("Try to get url for an invalid provider", func() {
_, err := a.Authorize("bitbucket", session, MockHttpRequest("http://localhost:2000/authy/bitbucket"))
So(err, ShouldNotEqual, nil)
})
Convey("Get access token from an invalid provider", func() {
_, _, err := a.Access("someone", session, MockHttpRequest("http://localhost:2000/"))
So(err, ShouldNotEqual, nil)
})
Convey("Get authorization url for provider", func() {
_, err := a.Authorize("github", session, MockHttpRequest("http://localhost:2000/authy/github"))
So(err, ShouldEqual, nil)
Convey("Get access token from provider", func() {
_, _, err := a.Access("github", session, MockHttpRequest("http://localhost:2000/authy/github/callback?code=auth_test&state="+url.QueryEscape(session.Get("authy.github.state").(string))))
So(err, ShouldEqual, nil)
Convey("State was deleted so second call should fail", func() {
_, _, err := a.Access("github", session, MockHttpRequest("http://localhost:2000/"))
So(err, ShouldNotEqual, nil)
server.Close()
})
})
})
})
}