-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathclient_test.go
191 lines (171 loc) · 4.43 KB
/
client_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
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
package ai_test
import (
"io"
"net/http"
"net/http/httptest"
"os"
"testing"
"github.com/philips-software/go-hsdp-api/ai"
"github.com/philips-software/go-hsdp-api/iam"
"github.com/stretchr/testify/assert"
)
var (
muxIAM *http.ServeMux
serverIAM *httptest.Server
muxIDM *http.ServeMux
serverIDM *httptest.Server
muxAI *http.ServeMux
serverAI *httptest.Server
iamClient *iam.Client
aiClient *ai.Client
aiTenantID = "48a0183d-a588-41c2-9979-737d15e9e860"
userUUID = "e7fecbb2-af8c-47c9-a662-5b046e048bc5"
)
func setup(t *testing.T) func() {
muxIAM = http.NewServeMux()
serverIAM = httptest.NewServer(muxIAM)
muxIDM = http.NewServeMux()
serverIDM = httptest.NewServer(muxIDM)
muxAI = http.NewServeMux()
serverAI = httptest.NewServer(muxAI)
var err error
iamClient, err = iam.NewClient(nil, &iam.Config{
OAuth2ClientID: "TestClient",
OAuth2Secret: "Secret",
SharedKey: "SharedKey",
SecretKey: "SecretKey",
IAMURL: serverIAM.URL,
IDMURL: serverIDM.URL,
})
if err != nil {
t.Fatalf("Failed to create iamCleitn: %v", err)
}
token := "44d20214-7879-4e35-923d-f9d4e01c9746"
muxIAM.HandleFunc("/authorize/oauth2/token", func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
t.Errorf("Expected ‘POST’ request")
w.WriteHeader(http.StatusBadRequest)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, _ = io.WriteString(w, `{
"scope": "mail tdr.contract tdr.dataitem",
"access_token": "`+token+`",
"refresh_token": "31f1a449-ef8e-4bfc-a227-4f2353fde547",
"expires_in": 1799,
"token_type": "Bearer"
}`)
})
muxIAM.HandleFunc("/authorize/oauth2/introspect", func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
t.Errorf("Expected ‘POST’ request")
w.WriteHeader(http.StatusBadRequest)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, _ = io.WriteString(w, `{
"active": true,
"scope": "auth_iam_organization auth_iam_introspect mail openid profile cn",
"username": "ronswanson",
"exp": 1592073485,
"sub": "`+userUUID+`",
"iss": "https://iam-Client-test.us-east.philips-healthsuite.com/oauth2/access_token",
"organizations": {
"managingOrganization": "`+aiTenantID+`",
"organizationList": [
{
"organizationId": "`+aiTenantID+`",
"permissions": [
"USER.READ",
"GROUP.WRITE",
"DEVICE.READ",
"CLIENT.SCOPES",
"AMS_ACCESS.ALL",
"PKI_CRL_CONFIGURATION.READ",
"PKI_CERT.ISSUE",
"PKI_CERT.READ",
"PKI_CERTS.LIST",
"PKI_CERTROLE.LIST",
"PKI_CERTROLE.READ",
"PKI_URLS.READ",
"PKI_CRL.ROTATE",
"PKI_CRL.CONFIGURE",
"PKI_CERT.SIGN",
"PKI_CERT.REVOKE",
"PKI_URLS.CONFIGURE"
],
"organizationName": "PawneeOrg",
"groups": [
"AdminGroup"
],
"roles": [
"ADMIN",
"PKIROLE"
]
}
]
},
"client_id": "testclientid",
"token_type": "Bearer",
"identity_type": "user"
}`)
})
// Login immediately so we can create tdrClient
err = iamClient.Login("username", "password")
assert.Nil(t, err)
aiClient, err = ai.NewClient(iamClient, &ai.Config{
BaseURL: serverAI.URL,
OrganizationID: aiTenantID,
Service: "inference",
})
if !assert.Nilf(t, err, "failed to create notificationClient: %v", err) {
return func() {
}
}
return func() {
serverIAM.Close()
serverIDM.Close()
serverAI.Close()
}
}
func TestLogin(t *testing.T) {
teardown := setup(t)
defer teardown()
token := "44d20214-7879-4e35-923d-f9d4e01c9746"
err := iamClient.Login("username", "password")
if err != nil {
t.Fatal(err)
}
accessToken, err := iamClient.Token()
if !assert.Nil(t, err) {
return
}
assert.Equal(t, token, accessToken)
}
func TestDebug(t *testing.T) {
teardown := setup(t)
defer teardown()
tempFile, err := os.CreateTemp("", "example")
if err != nil {
t.Fatalf("Error: %v", err)
}
aiClient, err = ai.NewClient(iamClient, &ai.Config{
BaseURL: serverAI.URL,
DebugLog: tempFile,
Service: "inference",
OrganizationID: "xxx",
})
if !assert.Nil(t, err) {
return
}
defer aiClient.Close()
defer func() {
_ = os.Remove(tempFile.Name())
}() // clean up
err = iamClient.Login("username", "password")
if !assert.Nil(t, err) {
return
}
}