| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 authdb | 5 package authdb |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "fmt" | 8 "fmt" |
| 9 "testing" | 9 "testing" |
| 10 | 10 |
| 11 "golang.org/x/net/context" | 11 "golang.org/x/net/context" |
| 12 | 12 |
| 13 "github.com/luci/gae/service/datastore" | 13 "github.com/luci/gae/service/datastore" |
| 14 "github.com/luci/luci-go/appengine/gaetesting" | 14 "github.com/luci/luci-go/appengine/gaetesting" |
| 15 "github.com/luci/luci-go/common/clock" | 15 "github.com/luci/luci-go/common/clock" |
| 16 "github.com/luci/luci-go/server/auth/service" | 16 "github.com/luci/luci-go/server/auth/service" |
| 17 "github.com/luci/luci-go/server/auth/service/protocol" | 17 "github.com/luci/luci-go/server/auth/service/protocol" |
| 18 | 18 |
| 19 . "github.com/smartystreets/goconvey/convey" | 19 . "github.com/smartystreets/goconvey/convey" |
| 20 ) | 20 ) |
| 21 | 21 |
| 22 func TestConfigureAuthService(t *testing.T) { | 22 func TestConfigureAuthService(t *testing.T) { |
| 23 Convey("Initial config", t, func() { | 23 Convey("Initial config", t, func() { |
| 24 srv := &fakeAuthService{LatestRev: 123} | 24 srv := &fakeAuthService{LatestRev: 123} |
| 25 c := setAuthService(gaetesting.TestingContext(), srv) | 25 c := setAuthService(gaetesting.TestingContext(), srv) |
| 26 | 26 |
| 27 So(ConfigureAuthService(c, "http://base_url", "http://auth-servi
ce"), ShouldBeNil) | 27 So(ConfigureAuthService(c, "http://base_url", "http://auth-servi
ce"), ShouldBeNil) |
| 28 So(srv.Calls, ShouldResemble, []string{ | 28 So(srv.Calls, ShouldResemble, []string{ |
| 29 » » » `EnsureSubscription "projects/dev~app/subscriptions/dev-
app-server-v1+auth-service" ""`, | 29 » » » `EnsureSubscription "projects/app/subscriptions/dev-app-
server-v1+auth-service" ""`, |
| 30 }) | 30 }) |
| 31 | 31 |
| 32 info, err := GetLatestSnapshotInfo(c) | 32 info, err := GetLatestSnapshotInfo(c) |
| 33 So(err, ShouldBeNil) | 33 So(err, ShouldBeNil) |
| 34 So(info, ShouldResemble, &SnapshotInfo{ | 34 So(info, ShouldResemble, &SnapshotInfo{ |
| 35 AuthServiceURL: "http://auth-service", | 35 AuthServiceURL: "http://auth-service", |
| 36 Rev: 123, | 36 Rev: 123, |
| 37 }) | 37 }) |
| 38 | 38 |
| 39 // Coverage for GetAuthDBSnapshot. | 39 // Coverage for GetAuthDBSnapshot. |
| 40 _, err = GetAuthDBSnapshot(c, "missing") | 40 _, err = GetAuthDBSnapshot(c, "missing") |
| 41 So(err, ShouldEqual, datastore.ErrNoSuchEntity) | 41 So(err, ShouldEqual, datastore.ErrNoSuchEntity) |
| 42 snap, err := GetAuthDBSnapshot(c, info.GetSnapshotID()) | 42 snap, err := GetAuthDBSnapshot(c, info.GetSnapshotID()) |
| 43 So(err, ShouldBeNil) | 43 So(err, ShouldBeNil) |
| 44 So(snap, ShouldResemble, &protocol.AuthDB{ | 44 So(snap, ShouldResemble, &protocol.AuthDB{ |
| 45 OauthClientId: strPtr("client-id-for-rev-123"), | 45 OauthClientId: strPtr("client-id-for-rev-123"), |
| 46 OauthClientSecret: strPtr("secret"), | 46 OauthClientSecret: strPtr("secret"), |
| 47 }) | 47 }) |
| 48 | 48 |
| 49 // Same config call again triggers resubsciption. | 49 // Same config call again triggers resubsciption. |
| 50 srv.Calls = nil | 50 srv.Calls = nil |
| 51 So(ConfigureAuthService(c, "http://base_url", "http://auth-servi
ce"), ShouldBeNil) | 51 So(ConfigureAuthService(c, "http://base_url", "http://auth-servi
ce"), ShouldBeNil) |
| 52 So(srv.Calls, ShouldResemble, []string{ | 52 So(srv.Calls, ShouldResemble, []string{ |
| 53 » » » `EnsureSubscription "projects/dev~app/subscriptions/dev-
app-server-v1+auth-service" ""`, | 53 » » » `EnsureSubscription "projects/app/subscriptions/dev-app-
server-v1+auth-service" ""`, |
| 54 }) | 54 }) |
| 55 }) | 55 }) |
| 56 | 56 |
| 57 Convey("Switching cfg", t, func() { | 57 Convey("Switching cfg", t, func() { |
| 58 srv := &fakeAuthService{LatestRev: 123} | 58 srv := &fakeAuthService{LatestRev: 123} |
| 59 c := setAuthService(gaetesting.TestingContext(), srv) | 59 c := setAuthService(gaetesting.TestingContext(), srv) |
| 60 | 60 |
| 61 // Initial config. | 61 // Initial config. |
| 62 So(ConfigureAuthService(c, "http://base_url", "http://auth-servi
ce-1"), ShouldBeNil) | 62 So(ConfigureAuthService(c, "http://base_url", "http://auth-servi
ce-1"), ShouldBeNil) |
| 63 // Change URL of the service. | 63 // Change URL of the service. |
| 64 So(ConfigureAuthService(c, "http://base_url", "http://auth-servi
ce-2"), ShouldBeNil) | 64 So(ConfigureAuthService(c, "http://base_url", "http://auth-servi
ce-2"), ShouldBeNil) |
| 65 | 65 |
| 66 info, err := GetLatestSnapshotInfo(c) | 66 info, err := GetLatestSnapshotInfo(c) |
| 67 So(err, ShouldBeNil) | 67 So(err, ShouldBeNil) |
| 68 So(info, ShouldResemble, &SnapshotInfo{ | 68 So(info, ShouldResemble, &SnapshotInfo{ |
| 69 AuthServiceURL: "http://auth-service-2", | 69 AuthServiceURL: "http://auth-service-2", |
| 70 Rev: 123, | 70 Rev: 123, |
| 71 }) | 71 }) |
| 72 | 72 |
| 73 So(srv.Calls, ShouldResemble, []string{ | 73 So(srv.Calls, ShouldResemble, []string{ |
| 74 » » » `EnsureSubscription "projects/dev~app/subscriptions/dev-
app-server-v1+auth-service-1" ""`, | 74 » » » `EnsureSubscription "projects/app/subscriptions/dev-app-
server-v1+auth-service-1" ""`, |
| 75 » » » `EnsureSubscription "projects/dev~app/subscriptions/dev-
app-server-v1+auth-service-2" ""`, | 75 » » » `EnsureSubscription "projects/app/subscriptions/dev-app-
server-v1+auth-service-2" ""`, |
| 76 » » » `DeleteSubscription "projects/dev~app/subscriptions/dev-
app-server-v1+auth-service-1"`, | 76 » » » `DeleteSubscription "projects/app/subscriptions/dev-app-
server-v1+auth-service-1"`, |
| 77 }) | 77 }) |
| 78 }) | 78 }) |
| 79 | 79 |
| 80 Convey("Removing cfg", t, func() { | 80 Convey("Removing cfg", t, func() { |
| 81 srv := &fakeAuthService{LatestRev: 123} | 81 srv := &fakeAuthService{LatestRev: 123} |
| 82 c := setAuthService(gaetesting.TestingContext(), srv) | 82 c := setAuthService(gaetesting.TestingContext(), srv) |
| 83 | 83 |
| 84 // Initial config. | 84 // Initial config. |
| 85 So(ConfigureAuthService(c, "http://base_url", "http://auth-servi
ce-1"), ShouldBeNil) | 85 So(ConfigureAuthService(c, "http://base_url", "http://auth-servi
ce-1"), ShouldBeNil) |
| 86 // Remove. | 86 // Remove. |
| 87 So(ConfigureAuthService(c, "http://base_url", ""), ShouldBeNil) | 87 So(ConfigureAuthService(c, "http://base_url", ""), ShouldBeNil) |
| 88 | 88 |
| 89 info, err := GetLatestSnapshotInfo(c) | 89 info, err := GetLatestSnapshotInfo(c) |
| 90 So(err, ShouldBeNil) | 90 So(err, ShouldBeNil) |
| 91 So(info, ShouldBeNil) | 91 So(info, ShouldBeNil) |
| 92 | 92 |
| 93 So(srv.Calls, ShouldResemble, []string{ | 93 So(srv.Calls, ShouldResemble, []string{ |
| 94 » » » `EnsureSubscription "projects/dev~app/subscriptions/dev-
app-server-v1+auth-service-1" ""`, | 94 » » » `EnsureSubscription "projects/app/subscriptions/dev-app-
server-v1+auth-service-1" ""`, |
| 95 » » » `DeleteSubscription "projects/dev~app/subscriptions/dev-
app-server-v1+auth-service-1"`, | 95 » » » `DeleteSubscription "projects/app/subscriptions/dev-app-
server-v1+auth-service-1"`, |
| 96 }) | 96 }) |
| 97 }) | 97 }) |
| 98 } | 98 } |
| 99 | 99 |
| 100 func TestSyncAuthDB(t *testing.T) { | 100 func TestSyncAuthDB(t *testing.T) { |
| 101 Convey("No new changes", t, func() { | 101 Convey("No new changes", t, func() { |
| 102 srv := &fakeAuthService{LatestRev: 123} | 102 srv := &fakeAuthService{LatestRev: 123} |
| 103 c := setAuthService(gaetesting.TestingContext(), srv) | 103 c := setAuthService(gaetesting.TestingContext(), srv) |
| 104 So(ConfigureAuthService(c, "http://base_url", "http://auth-servi
ce"), ShouldBeNil) | 104 So(ConfigureAuthService(c, "http://base_url", "http://auth-servi
ce"), ShouldBeNil) |
| 105 | 105 |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 169 } | 169 } |
| 170 return &service.Snapshot{ | 170 return &service.Snapshot{ |
| 171 AuthDB: &protocol.AuthDB{ | 171 AuthDB: &protocol.AuthDB{ |
| 172 OauthClientId: strPtr(fmt.Sprintf("client-id-for-rev
-%d", f.LatestRev)), | 172 OauthClientId: strPtr(fmt.Sprintf("client-id-for-rev
-%d", f.LatestRev)), |
| 173 OauthClientSecret: strPtr("secret"), | 173 OauthClientSecret: strPtr("secret"), |
| 174 }, | 174 }, |
| 175 Rev: f.LatestRev, | 175 Rev: f.LatestRev, |
| 176 Created: clock.Now(c), | 176 Created: clock.Now(c), |
| 177 }, nil | 177 }, nil |
| 178 } | 178 } |
| OLD | NEW |