forked from canonical/snapd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuserinfo_test.go
153 lines (129 loc) · 5.19 KB
/
userinfo_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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2016 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package store_test
import (
"fmt"
"net/http"
"net/http/httptest"
"os"
"time"
"gopkg.in/check.v1"
"gopkg.in/retry.v1"
"github.com/snapcore/snapd/logger"
"github.com/snapcore/snapd/store"
"github.com/snapcore/snapd/testutil"
)
type userInfoSuite struct {
testutil.BaseTest
store *store.Store
restoreLogger func()
}
var _ = check.Suite(&userInfoSuite{})
// obtained via:
//
// `curl https://login.staging.ubuntu.com/api/v2/keys/mvo@ubuntu.com`
// `curl https://login.staging.ubuntu.com/api/v2/keys/xDPXBdB`
var mockServerJSON = `{
"username": "mvo",
"ssh_keys": [
"ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAIEAqwsTkky+laeukWyGFmtiAQUFgjD+wKYuRtOj11gjTe3qUNDgMR54W8IUELZ6NwNWs2wium+jQZLY4vlsDq4PkYK8J2qgjRZURCKp4JbjbVNSg2WO7vDtl+0FIC1GaCdglRVWffrwKN1RLlwqBCVXi01nnTk3+hEpWddjqoTXMwM= egon@top",
"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDKBFmfD1KNULZv35907+ArIfxdGGzF1XCQj287AgK7k5GWcEdnUQfkSUHRZ4cNOqshY6W3CyDzVAmaDmeB9A7qpmsVlQp2D8y253+F2NMm1bcDdT3weG5vxkdF5qdx99gRMwDYJ4WZgIryrCAOqDLKmoSEuyuh1Zil9pDGPh/grf+EgXzDFnntgE8XJVKIldsbUplCmycSNtk47PtJATJ8q5v2dIazlxwmxKfarXS7x805u4ElrZ2h3JMCOOfL1k3sJbYc4JbZ6zB8DAhSsZ79KrStn3DE+gULmPJjM0HEbtouegZpE5wcHldoo4Oi78uNrwtv1lWp4AnK/Xwm3bl/ egon@bod\r\n"
],
"openid_identifier": "xDPXBdB"
}`
var mockServerNoSSHJSON = `{
"username": "mvo",
"openid_identifier": "xDPXBdB"
}`
func (t *userInfoSuite) SetUpTest(c *check.C) {
t.BaseTest.SetUpTest(c)
_, t.restoreLogger = logger.MockLogger()
t.store = store.New(nil, nil)
store.MockDefaultRetryStrategy(&t.BaseTest, retry.LimitCount(6, retry.LimitTime(1*time.Second,
retry.Exponential{
Initial: 1 * time.Millisecond,
Factor: 1.1,
},
)))
}
func (t *userInfoSuite) TearDownTest(c *check.C) {
t.BaseTest.TearDownTest(c)
t.restoreLogger()
}
func (s *userInfoSuite) redirectToTestSSO(handler func(http.ResponseWriter, *http.Request)) {
server := httptest.NewServer(http.HandlerFunc(handler))
s.BaseTest.AddCleanup(func() { server.Close() })
os.Setenv("SNAPPY_FORCE_SSO_URL", server.URL+"/api/v2")
s.BaseTest.AddCleanup(func() { os.Unsetenv("SNAPPY_FORCE_SSO_URL") })
}
func (s *userInfoSuite) TestCreateUserNoSSHKeys(c *check.C) {
n := 0
s.redirectToTestSSO(func(w http.ResponseWriter, r *http.Request) {
switch n {
case 0, 1:
w.WriteHeader(500) // force retry of the request
case 2:
c.Check(r.Method, check.Equals, "GET")
c.Check(r.URL.Path, check.Equals, "/api/v2/keys/popper@lse.ac.uk")
fmt.Fprintln(w, mockServerNoSSHJSON)
default:
c.Fatalf("expected to get 1 requests, now on %d", n+1)
}
n++
})
info, err := s.store.UserInfo("popper@lse.ac.uk")
c.Assert(err, check.IsNil)
c.Assert(n, check.Equals, 3) // number of requests after retries
c.Check(info.Username, check.Equals, "mvo")
c.Check(info.OpenIDIdentifier, check.Equals, "xDPXBdB")
c.Check(info.SSHKeys, check.HasLen, 0)
}
func (s *userInfoSuite) TestCreateUser(c *check.C) {
n := 0
s.redirectToTestSSO(func(w http.ResponseWriter, r *http.Request) {
switch n {
case 0, 1:
w.WriteHeader(500) // force retry of the request
case 2:
c.Check(r.Method, check.Equals, "GET")
c.Check(r.URL.Path, check.Equals, "/api/v2/keys/popper@lse.ac.uk")
fmt.Fprintln(w, mockServerJSON)
default:
c.Fatalf("expected to get 1 requests, now on %d", n+1)
}
n++
})
info, err := s.store.UserInfo("popper@lse.ac.uk")
c.Assert(err, check.IsNil)
c.Assert(n, check.Equals, 3) // number of requests after retries
c.Check(info.Username, check.Equals, "mvo")
c.Check(info.OpenIDIdentifier, check.Equals, "xDPXBdB")
c.Check(info.SSHKeys, check.DeepEquals, []string{"ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAIEAqwsTkky+laeukWyGFmtiAQUFgjD+wKYuRtOj11gjTe3qUNDgMR54W8IUELZ6NwNWs2wium+jQZLY4vlsDq4PkYK8J2qgjRZURCKp4JbjbVNSg2WO7vDtl+0FIC1GaCdglRVWffrwKN1RLlwqBCVXi01nnTk3+hEpWddjqoTXMwM= egon@top",
"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDKBFmfD1KNULZv35907+ArIfxdGGzF1XCQj287AgK7k5GWcEdnUQfkSUHRZ4cNOqshY6W3CyDzVAmaDmeB9A7qpmsVlQp2D8y253+F2NMm1bcDdT3weG5vxkdF5qdx99gRMwDYJ4WZgIryrCAOqDLKmoSEuyuh1Zil9pDGPh/grf+EgXzDFnntgE8XJVKIldsbUplCmycSNtk47PtJATJ8q5v2dIazlxwmxKfarXS7x805u4ElrZ2h3JMCOOfL1k3sJbYc4JbZ6zB8DAhSsZ79KrStn3DE+gULmPJjM0HEbtouegZpE5wcHldoo4Oi78uNrwtv1lWp4AnK/Xwm3bl/ egon@bod\r\n"})
}
func (s *userInfoSuite) TestCreateUser500RetriesExhausted(c *check.C) {
n := 0
s.redirectToTestSSO(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(500)
n++
})
_, err := s.store.UserInfo("popper@lse.ac.uk")
c.Assert(err, check.NotNil)
c.Assert(err, check.ErrorMatches, `cannot look up user.*?got unexpected HTTP status code 500.*`)
c.Assert(n, check.Equals, 6)
}