This repository has been archived by the owner on Mar 11, 2021. It is now read-only.
/
user_deactivation_e2e_base.go
249 lines (221 loc) · 7.72 KB
/
user_deactivation_e2e_base.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package integration
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"strings"
"testing"
"time"
"github.com/fabric8-services/fabric8-auth/gormtestsupport"
testsupport "github.com/fabric8-services/fabric8-auth/test"
testtoken "github.com/fabric8-services/fabric8-auth/test/token"
"github.com/stretchr/testify/require"
)
var (
authBinary = "../../bin/auth"
ago7days = time.Now().Add(-7 * 24 * time.Hour)
ago40days = time.Now().Add(-40 * 24 * time.Hour)
)
type BaseSuite struct {
gormtestsupport.DBTestSuite
witServer *httptest.Server
notificationServer *httptest.Server
tenantServer *httptest.Server
clusterServer *httptest.Server
cheServer *httptest.Server
regAppServer *httptest.Server
stopAuth func()
displayErrors func(t *testing.T)
}
func (s *BaseSuite) SetupTest(notificationDone, deactivateDone chan string) {
s.DBTestSuite.SetupTest()
// start mock server
s.witServer = startServer(newWITServer(), 8080)
s.notificationServer = startServer(newNotificationServer(notificationDone), 8082)
s.tenantServer = startServer(newTenantServer(), 8090)
s.clusterServer = startServer(newClusterServer(), 8083)
s.cheServer = startServer(newCheServer(), 8091)
s.regAppServer = startServer(newRegAppServer(deactivateDone), 8085)
// start auth_service
configFile := os.Getenv("AUTH_CONFIG_FILE_PATH")
os.Setenv("AUTH_CONFIG_FILE_PATH", "e2e_test_config.yml")
var cmd *exec.Cmd
cmd, s.displayErrors = s.runAuthService()
err := cmd.Start()
require.NoError(s.T(), err)
s.stopAuth = func() {
_ = cmd.Process.Kill()
log.Println("[Test runner] Auth service stopped")
os.Setenv("AUTH_CONFIG_FILE_PATH", configFile)
log.Println("[Test runner] Restored default config file in env")
}
log.Println("[Test runner] Auth service started")
}
func (s *BaseSuite) TearDownTest() {
s.DBTestSuite.TearDownTest()
s.displayErrors(s.T())
// stop mock server
stopServer(s.witServer)
stopServer(s.notificationServer)
stopServer(s.tenantServer)
stopServer(s.clusterServer)
stopServer(s.cheServer)
stopServer(s.regAppServer)
s.stopAuth()
}
func (s *BaseSuite) runAuthService(args ...string) (*exec.Cmd, func(*testing.T)) {
cmd := exec.Command(authBinary, args...)
var out bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &out
return cmd, func(t *testing.T) {
if t.Failed() {
displayAuthLogs(t, out)
}
}
}
func newRegAppServer(DeactivateDone chan string) func(rw http.ResponseWriter, r *http.Request) {
saToken, _ := testtoken.TokenManager.GenerateServiceAccountToken(testsupport.TestOnlineRegistrationAppIdentity.ID.String(),
testsupport.TestOnlineRegistrationAppIdentity.Username)
return func(rw http.ResponseWriter, r *http.Request) {
log.Printf("[RegApp service] incoming request: %s %s\n", r.Method, r.URL.Path)
username := extractUsername(r.URL.Path)
url := fmt.Sprintf("http://127.0.0.1:8089/api/namedusers/%s/deactivate", username)
req, _ := http.NewRequest(http.MethodPatch, url, nil)
tokenHeader := fmt.Sprintf("Bearer %s", saToken)
req.Header.Set("Authorization", tokenHeader)
httpClient := http.Client{}
res, err := httpClient.Do(req)
if err != nil {
log.Printf("[RegApp service] error occurred: %v\n", err)
rw.WriteHeader(http.StatusInternalServerError)
log.Printf("[RegApp service] returning response with status: %d\n", http.StatusInternalServerError)
return
}
userID := extractIDFromRegAppRequestPath(r.URL.Path)
DeactivateDone <- userID
log.Printf("[RegApp service] returning response with status: %d\n", res.StatusCode)
}
}
func newNotificationServer(notificationDone chan string) func(rw http.ResponseWriter, r *http.Request) {
return func(rw http.ResponseWriter, r *http.Request) {
log.Printf("[Notification service] incoming request: %s %s\n", r.Method, r.URL.Path)
defer r.Body.Close()
content, _ := ioutil.ReadAll(r.Body)
identityID := extractIDFromNotificationPayload(content)
notificationDone <- identityID
rw.WriteHeader(http.StatusAccepted)
log.Printf("[Notification service] returning response with status: %d\n", http.StatusAccepted)
}
}
func newClusterServer() func(rw http.ResponseWriter, r *http.Request) {
return func(rw http.ResponseWriter, r *http.Request) {
log.Printf("[Cluster service] incoming request: %s %s\n", r.Method, r.URL.Path)
_, _ = rw.Write([]byte(`{
"data": [
{
"api-url": "starter-us-east-2",
"app-dns": "b542.starter-us-east-2a.openshiftapps.com",
"auth-client-default-scope": "user:full",
"auth-client-id": "openshift-io",
"auth-client-secret": "26c8c584-cbac-427d-8330-8b430b6ec620",
"capacity-exhausted": false,
"name": "starter-us-east-2a",
"service-account-token": "eef1c5b8-f1f4-45dd-beef-7c34be5d9f9b",
"service-account-username": "devtools-sre",
"token-provider-id": "dd0ee660-3549-4617-9cab-6e679aab41e9"
}
]
}`))
log.Printf("[Cluster service] returning response with status: %d\n", http.StatusOK)
}
}
func newWITServer() func(rw http.ResponseWriter, r *http.Request) {
return func(rw http.ResponseWriter, r *http.Request) {
log.Printf("[WIT service] incoming request: %s %s\n", r.Method, r.URL.Path)
log.Printf("[WIT service] returning response with status: %d\n", http.StatusOK)
rw.WriteHeader(http.StatusOK)
}
}
func newTenantServer() func(rw http.ResponseWriter, r *http.Request) {
return func(rw http.ResponseWriter, r *http.Request) {
log.Printf("[Tenant service] incoming request: %s %s\n", r.Method, r.URL.Path)
log.Printf("[Tenant service] returning response with status: %d\n", http.StatusNoContent)
rw.WriteHeader(http.StatusNoContent)
}
}
func newCheServer() func(rw http.ResponseWriter, r *http.Request) {
return func(rw http.ResponseWriter, r *http.Request) {
log.Printf("[Che service] incoming request: %s %s\n", r.Method, r.URL.Path)
log.Printf("[Che service] returning response with status: %d\n", http.StatusNoContent)
rw.WriteHeader(http.StatusNoContent)
}
}
func displayAuthLogs(t *testing.T, output bytes.Buffer) {
log.Println("***********************************************************")
log.Println("-------------------- Auth Service Logs --------------------")
log.Println("***********************************************************")
log.Println(output.String())
log.Println("***********************************************************")
}
func startServer(handler func(w http.ResponseWriter, r *http.Request), port int) (ts *httptest.Server) {
if handler == nil {
handler = func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "port=%d", port)
}
}
if listener, err := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", port)); err != nil {
panic(err)
} else {
ts = &httptest.Server{
Listener: listener,
Config: &http.Server{Handler: http.HandlerFunc(handler)},
}
ts.Start()
}
return
}
func stopServer(server *httptest.Server) {
if server != nil {
server.Close()
}
}
// helper
type Payload struct {
Data *Data
}
type Data struct {
Attributes *Attributes
}
type Attributes struct {
ID string
}
func extractIDFromNotificationPayload(content []byte) string {
var payload Payload
err := json.Unmarshal([]byte(content), &payload)
if err != nil {
return ""
}
if payload.Data == nil {
return ""
}
return payload.Data.Attributes.ID
}
func extractIDFromRegAppRequestPath(path string) string {
// /api/namedusers/TestUserIdentity-1111/deprovision_osio // here `1111` is ID to be extracted
startInd := strings.Index(path, "/TestUserIdentity-") + len("/TestUserIdentity-")
endInd := strings.Index(path, "/deprovision_osio")
return path[startInd:endInd]
}
func extractUsername(path string) string {
startInd := strings.Index(path, "/api/accounts/") + len("/api/accounts/")
endInd := strings.Index(path, "/deprovision_osio")
return path[startInd:endInd]
}