| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package auth | |
| 6 | |
| 7 import ( | |
| 8 "io/ioutil" | |
| 9 "net/http" | |
| 10 "strings" | |
| 11 "testing" | |
| 12 "time" | |
| 13 | |
| 14 . "github.com/smartystreets/goconvey/convey" | |
| 15 ) | |
| 16 | |
| 17 func TestGroupsService(t *testing.T) { | |
| 18 Convey("Mocking time and HTTP", t, func() { | |
| 19 mockSleep() | |
| 20 | |
| 21 requests := mockDoRequest() | |
| 22 service := NewGroupsService("https://example.com", &http.Client{
}, nil) | |
| 23 | |
| 24 Convey("doGet works", func() { | |
| 25 requests.expect("https://example.com/some/path", 200, `{
"key":"val"}`) | |
| 26 var response struct { | |
| 27 Key string `json:"key"` | |
| 28 } | |
| 29 err := service.doGet("/some/path", &response) | |
| 30 So(err, ShouldBeNil) | |
| 31 So(response.Key, ShouldEqual, "val") | |
| 32 }) | |
| 33 | |
| 34 Convey("doGet retry works", func() { | |
| 35 requests.expect("https://example.com/some/path", 500, ""
) | |
| 36 requests.expect("https://example.com/some/path", 503, ""
) | |
| 37 requests.expect("https://example.com/some/path", 200, `{
"key":"val"}`) | |
| 38 var response struct { | |
| 39 Key string `json:"key"` | |
| 40 } | |
| 41 err := service.doGet("/some/path", &response) | |
| 42 So(err, ShouldBeNil) | |
| 43 So(response.Key, ShouldEqual, "val") | |
| 44 }) | |
| 45 | |
| 46 Convey("doGet fatal error", func() { | |
| 47 requests.expect("https://example.com/some/path", 403, "e
rror") | |
| 48 var response struct{} | |
| 49 err := service.doGet("/some/path", &response) | |
| 50 So(err, ShouldNotBeNil) | |
| 51 }) | |
| 52 | |
| 53 Convey("doGet max retry", func() { | |
| 54 for i := 0; i < 5; i++ { | |
| 55 requests.expect("https://example.com/some/path",
500, "") | |
| 56 } | |
| 57 var response struct{} | |
| 58 err := service.doGet("/some/path", &response) | |
| 59 So(err, ShouldNotBeNil) | |
| 60 }) | |
| 61 | |
| 62 Convey("FetchCallerIdentity works", func() { | |
| 63 requests.expect("https://example.com/auth/api/v1/account
s/self", 200, `{ | |
| 64 "identity": "user:abc@example.com" | |
| 65 }`) | |
| 66 ident, err := service.FetchCallerIdentity() | |
| 67 So(err, ShouldBeNil) | |
| 68 So(ident, ShouldResemble, Identity{ | |
| 69 Kind: IdentityKindUser, | |
| 70 Name: "abc@example.com", | |
| 71 }) | |
| 72 }) | |
| 73 | |
| 74 Convey("FetchGroup works", func() { | |
| 75 requests.expect("https://example.com/auth/api/v1/groups/
abc/name", 200, `{ | |
| 76 "group": { | |
| 77 "name": "abc/name", | |
| 78 "description": "Some description", | |
| 79 "members": [ | |
| 80 "anonymous:anonymous", | |
| 81 "bot:127.0.0.1", | |
| 82 "service:some-gae-app", | |
| 83 "user:abc@example.com", | |
| 84 "broken id", | |
| 85 "unknown_type:abc" | |
| 86 ], | |
| 87 "globs": ["user:*@example.com"], | |
| 88 "nested": ["another-group"] | |
| 89 } | |
| 90 }`) | |
| 91 group, err := service.FetchGroup("abc/name") | |
| 92 So(err, ShouldBeNil) | |
| 93 So(group, ShouldResemble, Group{ | |
| 94 Name: "abc/name", | |
| 95 Description: "Some description", | |
| 96 Members: []Identity{ | |
| 97 Identity{Kind: IdentityKindAnonymous, Na
me: "anonymous"}, | |
| 98 Identity{Kind: IdentityKindBot, Name: "1
27.0.0.1"}, | |
| 99 Identity{Kind: IdentityKindService, Name
: "some-gae-app"}, | |
| 100 Identity{Kind: IdentityKindUser, Name: "
abc@example.com"}, | |
| 101 }, | |
| 102 Globs: []string{"user:*@example.com"}, | |
| 103 Nested: []string{"another-group"}, | |
| 104 }) | |
| 105 }) | |
| 106 }) | |
| 107 } | |
| 108 | |
| 109 func mockSleep() { | |
| 110 prev := sleep | |
| 111 sleep = func(d time.Duration) {} | |
| 112 Reset(func() { | |
| 113 sleep = prev | |
| 114 }) | |
| 115 } | |
| 116 | |
| 117 type mockedRequest struct { | |
| 118 URL string | |
| 119 StatusCode int | |
| 120 Response string | |
| 121 } | |
| 122 | |
| 123 type mockedRequests struct { | |
| 124 Expected []mockedRequest | |
| 125 } | |
| 126 | |
| 127 func mockDoRequest() *mockedRequests { | |
| 128 requests := &mockedRequests{} | |
| 129 prev := doRequest | |
| 130 doRequest = func(c *http.Client, req *http.Request) (*http.Response, err
or) { | |
| 131 So(len(requests.Expected), ShouldNotEqual, 0) | |
| 132 next := requests.Expected[0] | |
| 133 requests.Expected = requests.Expected[1:] | |
| 134 So(next.URL, ShouldEqual, req.URL.String()) | |
| 135 return &http.Response{ | |
| 136 StatusCode: next.StatusCode, | |
| 137 Body: ioutil.NopCloser(strings.NewReader(next.Resp
onse)), | |
| 138 }, nil | |
| 139 } | |
| 140 Reset(func() { | |
| 141 requests.assertExecuted() | |
| 142 doRequest = prev | |
| 143 }) | |
| 144 return requests | |
| 145 } | |
| 146 | |
| 147 func (r *mockedRequests) expect(url string, status int, response string) { | |
| 148 r.Expected = append(r.Expected, mockedRequest{ | |
| 149 URL: url, | |
| 150 StatusCode: status, | |
| 151 Response: response, | |
| 152 }) | |
| 153 } | |
| 154 | |
| 155 func (r *mockedRequests) assertExecuted() { | |
| 156 So(r.Expected, ShouldBeEmpty) | |
| 157 } | |
| OLD | NEW |