| OLD | NEW |
| 1 // Copyright 2017 The LUCI Authors. All rights reserved. | 1 // Copyright 2017 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
| 4 | 4 |
| 5 package localauth | 5 package localauth |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "testing" | 8 "testing" |
| 9 "time" | 9 "time" |
| 10 | 10 |
| 11 "golang.org/x/net/context" | 11 "golang.org/x/net/context" |
| 12 "golang.org/x/oauth2" | 12 "golang.org/x/oauth2" |
| 13 | 13 |
| 14 "github.com/luci/luci-go/common/clock" | 14 "github.com/luci/luci-go/common/clock" |
| 15 "github.com/luci/luci-go/common/clock/testclock" | 15 "github.com/luci/luci-go/common/clock/testclock" |
| 16 "github.com/luci/luci-go/lucictx" | 16 "github.com/luci/luci-go/lucictx" |
| 17 | 17 |
| 18 . "github.com/smartystreets/goconvey/convey" | 18 . "github.com/smartystreets/goconvey/convey" |
| 19 ) | 19 ) |
| 20 | 20 |
| 21 func TestWithLocalAuth(t *testing.T) { | 21 func TestWithLocalAuth(t *testing.T) { |
| 22 t.Parallel() | 22 t.Parallel() |
| 23 | 23 |
| 24 ctx := context.Background() | 24 ctx := context.Background() |
| 25 ctx, _ = testclock.UseTime(ctx, testclock.TestRecentTimeLocal) | 25 ctx, _ = testclock.UseTime(ctx, testclock.TestRecentTimeLocal) |
| 26 | 26 |
| 27 gen := func(ctx context.Context, scopes []string, lifetime time.Duration
) (*oauth2.Token, error) { |
| 28 return &oauth2.Token{ |
| 29 AccessToken: "tok", |
| 30 Expiry: clock.Now(ctx).Add(30 * time.Minute), |
| 31 }, nil |
| 32 } |
| 33 |
| 27 srv := Server{ | 34 srv := Server{ |
| 28 » » TokenGenerator: func(ctx context.Context, scopes []string, lifet
ime time.Duration) (*oauth2.Token, error) { | 35 » » TokenGenerators: map[string]TokenGenerator{"acc_id": gen}, |
| 29 » » » return &oauth2.Token{ | |
| 30 » » » » AccessToken: "tok", | |
| 31 » » » » Expiry: clock.Now(ctx).Add(30 * time.Minute
), | |
| 32 » » » }, nil | |
| 33 » » }, | |
| 34 } | 36 } |
| 35 | 37 |
| 36 Convey("Works", t, func() { | 38 Convey("Works", t, func() { |
| 37 WithLocalAuth(ctx, &srv, func(ctx context.Context) error { | 39 WithLocalAuth(ctx, &srv, func(ctx context.Context) error { |
| 38 p := lucictx.GetLocalAuth(ctx) | 40 p := lucictx.GetLocalAuth(ctx) |
| 39 So(p, ShouldNotBeNil) | 41 So(p, ShouldNotBeNil) |
| 40 req := prepReq(p, "/rpc/LuciLocalAuthService.GetOAuthTok
en", map[string]interface{}{ | 42 req := prepReq(p, "/rpc/LuciLocalAuthService.GetOAuthTok
en", map[string]interface{}{ |
| 41 » » » » "scopes": []string{"B", "A"}, | 43 » » » » "scopes": []string{"B", "A"}, |
| 42 » » » » "secret": p.Secret, | 44 » » » » "secret": p.Secret, |
| 45 » » » » "account_id": "acc_id", |
| 43 }) | 46 }) |
| 44 So(call(req), ShouldEqual, `HTTP 200 (json): {"access_to
ken":"tok","expiry":1454502906}`) | 47 So(call(req), ShouldEqual, `HTTP 200 (json): {"access_to
ken":"tok","expiry":1454502906}`) |
| 45 return nil | 48 return nil |
| 46 }) | 49 }) |
| 47 }) | 50 }) |
| 48 } | 51 } |
| OLD | NEW |