Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(666)

Side by Side Diff: go/src/infra/libs/auth/auth_test.go

Issue 1149313002: go: Replace Logrus with go-logging. (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@master
Patch Set: typo Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « go/src/infra/libs/auth/auth.go ('k') | go/src/infra/libs/logging/appengine.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package auth 5 package auth
6 6
7 import ( 7 import (
8 "io/ioutil" 8 "io/ioutil"
9 "net/http" 9 "net/http"
10 "os" 10 "os"
11 "path/filepath" 11 "path/filepath"
12 "testing" 12 "testing"
13 13
14 "infra/libs/auth/internal" 14 "infra/libs/auth/internal"
15 "infra/libs/logging" 15 "infra/libs/logging"
16 16
17 "golang.org/x/net/context" 17 "golang.org/x/net/context"
18 18
19 . "github.com/smartystreets/goconvey/convey" 19 . "github.com/smartystreets/goconvey/convey"
20 ) 20 )
21 21
22 var (
23 log = logging.DefaultLogger
24 )
25
22 func ExampleDefaultAuthenticatedClient() { 26 func ExampleDefaultAuthenticatedClient() {
23 client, err := DefaultAuthenticatedClient(SilentLogin) 27 client, err := DefaultAuthenticatedClient(SilentLogin)
24 if err == ErrLoginRequired { 28 if err == ErrLoginRequired {
25 » » logging.Errorf("Run 'auth login' to login") 29 » » log.Errorf("Run 'auth login' to login")
26 return 30 return
27 } 31 }
28 if err != nil { 32 if err != nil {
29 » » logging.Errorf("Failed to login: %s", err) 33 » » log.Errorf("Failed to login: %s", err)
30 return 34 return
31 } 35 }
32 client.Get("https://some-server.appspot.com") 36 client.Get("https://some-server.appspot.com")
33 } 37 }
34 38
35 func mockSecretsDir() string { 39 func mockSecretsDir() string {
36 tempDir, err := ioutil.TempDir("", "auth_test") 40 tempDir, err := ioutil.TempDir("", "auth_test")
37 So(err, ShouldBeNil) 41 So(err, ShouldBeNil)
38 42
39 prev := secretsDir 43 prev := secretsDir
(...skipping 10 matching lines...) Expand all
50 func mockTokenProvider(factory func() internal.TokenProvider) { 54 func mockTokenProvider(factory func() internal.TokenProvider) {
51 prev := makeTokenProvider 55 prev := makeTokenProvider
52 makeTokenProvider = func(*Options) (internal.TokenProvider, error) { 56 makeTokenProvider = func(*Options) (internal.TokenProvider, error) {
53 return factory(), nil 57 return factory(), nil
54 } 58 }
55 Reset(func() { 59 Reset(func() {
56 makeTokenProvider = prev 60 makeTokenProvider = prev
57 }) 61 })
58 } 62 }
59 63
60 func mockTerminal() {
61 prev := logging.IsTerminal
62 logging.IsTerminal = true
63 Reset(func() {
64 logging.IsTerminal = prev
65 })
66 }
67
68 func TestAuthenticator(t *testing.T) { 64 func TestAuthenticator(t *testing.T) {
69 Convey("Given mocked secrets dir", t, func() { 65 Convey("Given mocked secrets dir", t, func() {
70 tempDir := mockSecretsDir() 66 tempDir := mockSecretsDir()
71 67
72 Convey("Check NewAuthenticator defaults", func() { 68 Convey("Check NewAuthenticator defaults", func() {
73 clientID, clientSecret := DefaultClient() 69 clientID, clientSecret := DefaultClient()
74 a := NewAuthenticator(Options{}).(*authenticatorImpl) 70 a := NewAuthenticator(Options{}).(*authenticatorImpl)
75 So(a.opts, ShouldResemble, &Options{ 71 So(a.opts, ShouldResemble, &Options{
76 Method: AutoSelectMethod, 72 Method: AutoSelectMethod,
77 Scopes: []string{OAuthScopeEmail }, 73 Scopes: []string{OAuthScopeEmail },
(...skipping 29 matching lines...) Expand all
107 So(len(left), ShouldEqual, 1) 103 So(len(left), ShouldEqual, 1)
108 So(left[0].Name(), ShouldEqual, "not-secret.txt") 104 So(left[0].Name(), ShouldEqual, "not-secret.txt")
109 }) 105 })
110 }) 106 })
111 } 107 }
112 108
113 func TestAuthenticatedClient(t *testing.T) { 109 func TestAuthenticatedClient(t *testing.T) {
114 Convey("Given mocked secrets dir", t, func() { 110 Convey("Given mocked secrets dir", t, func() {
115 var tokenProvider internal.TokenProvider 111 var tokenProvider internal.TokenProvider
116 112
117 mockTerminal()
118 mockSecretsDir() 113 mockSecretsDir()
119 mockTokenProvider(func() internal.TokenProvider { return tokenPr ovider }) 114 mockTokenProvider(func() internal.TokenProvider { return tokenPr ovider })
120 115
121 Convey("Test login required", func() { 116 Convey("Test login required", func() {
122 tokenProvider = &fakeTokenProvider{interactive: true} 117 tokenProvider = &fakeTokenProvider{interactive: true}
123 c, err := AuthenticatedClient(InteractiveLogin, NewAuthe nticator(Options{})) 118 c, err := AuthenticatedClient(InteractiveLogin, NewAuthe nticator(Options{}))
124 So(err, ShouldBeNil) 119 So(err, ShouldBeNil)
125 So(c, ShouldNotEqual, http.DefaultClient) 120 So(c, ShouldNotEqual, http.DefaultClient)
126 }) 121 })
127 122
128 Convey("Test login not required", func() { 123 Convey("Test login not required", func() {
129 tokenProvider = &fakeTokenProvider{interactive: true} 124 tokenProvider = &fakeTokenProvider{interactive: true}
130 c, err := AuthenticatedClient(OptionalLogin, NewAuthenti cator(Options{})) 125 c, err := AuthenticatedClient(OptionalLogin, NewAuthenti cator(Options{}))
131 So(err, ShouldBeNil) 126 So(err, ShouldBeNil)
132 So(c, ShouldEqual, http.DefaultClient) 127 So(c, ShouldEqual, http.DefaultClient)
133 }) 128 })
134 }) 129 })
135 } 130 }
136 131
137 func TestRefreshToken(t *testing.T) { 132 func TestRefreshToken(t *testing.T) {
138 Convey("Given mocked secrets dir", t, func() { 133 Convey("Given mocked secrets dir", t, func() {
139 var tokenProvider *fakeTokenProvider 134 var tokenProvider *fakeTokenProvider
140 135
141 mockTerminal()
142 mockSecretsDir() 136 mockSecretsDir()
143 mockTokenProvider(func() internal.TokenProvider { return tokenPr ovider }) 137 mockTokenProvider(func() internal.TokenProvider { return tokenPr ovider })
144 138
145 Convey("Test non interactive auth", func() { 139 Convey("Test non interactive auth", func() {
146 tokenProvider = &fakeTokenProvider{ 140 tokenProvider = &fakeTokenProvider{
147 interactive: false, 141 interactive: false,
148 tokenToMint: &fakeToken{}, 142 tokenToMint: &fakeToken{},
149 } 143 }
150 auth, ok := NewAuthenticator(Options{}).(*authenticatorI mpl) 144 auth, ok := NewAuthenticator(Options{}).(*authenticatorI mpl)
151 So(ok, ShouldBeTrue) 145 So(ok, ShouldBeTrue)
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
249 expired bool 243 expired bool
250 } 244 }
251 245
252 func (t *fakeToken) Equals(another internal.Token) bool { 246 func (t *fakeToken) Equals(another internal.Token) bool {
253 casted, ok := another.(*fakeToken) 247 casted, ok := another.(*fakeToken)
254 return ok && casted == t 248 return ok && casted == t
255 } 249 }
256 250
257 func (t *fakeToken) RequestHeaders() map[string]string { return make(map[string] string) } 251 func (t *fakeToken) RequestHeaders() map[string]string { return make(map[string] string) }
258 func (t *fakeToken) Expired() bool { return t.expired } 252 func (t *fakeToken) Expired() bool { return t.expired }
OLDNEW
« no previous file with comments | « go/src/infra/libs/auth/auth.go ('k') | go/src/infra/libs/logging/appengine.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698