@@ -9,47 +9,45 @@ import (
99 "testing"
1010
1111 drycc "github.com/drycc/controller-sdk-go"
12+ "github.com/drycc/controller-sdk-go/api"
1213)
1314
14- const adminFixture string = `
15+ const codenamesFixture string = `
1516{
16- "count": 1,
17- "next": null,
18- "previous": null,
19- "results": [
20- {
21- "username": "test",
22- "is_superuser": true
23- },
24- {
25- "username": "foo",
26- "is_superuser": true
27- }
28- ]
17+ "results": [
18+ {"codename": "use_app", "description": "Can use app"},
19+ {"codename": "use_cert", "description": "Can use cert"}
20+ ],
21+ "count": 2
2922}`
3023
31- const appPermsFixture string = `
24+ const listUserPermFixture string = `
3225{
33- "users": [
34- "test",
35- "testing"
36- ]
26+ "results": [
27+ {"id": 1, "codename": "use_app", "uniqueid": "autotest-app", "username": "foo"},
28+ {"id": 2, "codename": "use_cert", "uniqueid": "autotest-cert-1", "username": "foo"}
29+ ],
30+ "count": 2
3731}`
3832
39- const adminCreateExpected = `{"username":"test"}`
40- const appCreateExpected = `{"username":"foo"}`
33+ const createUserPermExpected = `{"codename":"use_app","uniqueid":"autotest","username":"foo"}`
4134
4235type fakeHTTPServer struct {}
4336
4437func (fakeHTTPServer ) ServeHTTP (res http.ResponseWriter , req * http.Request ) {
4538 res .Header ().Add ("DRYCC_API_VERSION" , drycc .APIVersion )
4639
47- if req .URL .Path == "/v2/admin/ perms/" && req .Method == "GET" {
48- res .Write ([]byte (adminFixture ))
40+ if req .URL .Path == "/v2/perms/codes /" && req .Method == "GET" {
41+ res .Write ([]byte (codenamesFixture ))
4942 return
5043 }
5144
52- if req .URL .Path == "/v2/admin/perms/" && req .Method == "POST" {
45+ if req .URL .Path == "/v2/perms/rules/" && req .Method == "GET" {
46+ res .Write ([]byte (listUserPermFixture ))
47+ return
48+ }
49+
50+ if req .URL .Path == "/v2/perms/rules/" && req .Method == "POST" {
5351 body , err := io .ReadAll (req .Body )
5452
5553 if err != nil {
@@ -58,8 +56,8 @@ func (fakeHTTPServer) ServeHTTP(res http.ResponseWriter, req *http.Request) {
5856 res .Write (nil )
5957 }
6058
61- if string (body ) != adminCreateExpected {
62- fmt .Printf ("Expected '%s', Got '%s'\n " , adminCreateExpected , body )
59+ if string (body ) != createUserPermExpected {
60+ fmt .Printf ("Expected '%s', Got '%s'\n " , createUserPermExpected , body )
6361 res .WriteHeader (http .StatusInternalServerError )
6462 res .Write (nil )
6563 return
@@ -70,55 +68,23 @@ func (fakeHTTPServer) ServeHTTP(res http.ResponseWriter, req *http.Request) {
7068 return
7169 }
7270
73- if req .URL .Path == "/v2/admin/perms/test" && req .Method == "DELETE" {
74- res .WriteHeader (http .StatusNoContent )
75- res .Write (nil )
76- return
77- }
78-
79- if req .URL .Path == "/v2/apps/example-go/perms/" && req .Method == "GET" {
80- res .Write ([]byte (appPermsFixture ))
81- return
82- }
83-
84- if req .URL .Path == "/v2/apps/example-go/perms/foo" && req .Method == "DELETE" {
71+ if req .URL .Path == "/v2/perms/rules/1/" && req .Method == "DELETE" {
8572 res .WriteHeader (http .StatusNoContent )
8673 res .Write (nil )
8774 return
8875 }
8976
90- if req .URL .Path == "/v2/apps/example-go/perms/" && req .Method == "POST" {
91- body , err := io .ReadAll (req .Body )
92-
93- if err != nil {
94- fmt .Println (err )
95- res .WriteHeader (http .StatusInternalServerError )
96- res .Write (nil )
97- }
98-
99- if string (body ) != appCreateExpected {
100- fmt .Printf ("Expected '%s', Got '%s'\n " , appCreateExpected , body )
101- res .WriteHeader (http .StatusInternalServerError )
102- res .Write (nil )
103- return
104- }
105-
106- res .WriteHeader (http .StatusCreated )
107- res .Write (nil )
108- return
109- }
110-
11177 fmt .Printf ("Unrecognized URL %s\n " , req .URL )
11278 res .WriteHeader (http .StatusNotFound )
11379 res .Write (nil )
11480}
11581
116- func TestList (t * testing.T ) {
82+ func TestNames (t * testing.T ) {
11783 t .Parallel ()
11884
119- expected := []string {
120- "test" ,
121- "testing" ,
85+ expected := []api. PermCodeResponse {
86+ { Codename : "use_app" , Description : "Can use app" } ,
87+ { Codename : "use_cert" , Description : "Can use cert" } ,
12288 }
12389
12490 handler := fakeHTTPServer {}
@@ -130,7 +96,7 @@ func TestList(t *testing.T) {
13096 t .Fatal (err )
13197 }
13298
133- actual , err := List (drycc , "example-go" )
99+ actual , _ , err := Codes (drycc , 300 )
134100
135101 if err != nil {
136102 t .Fatal (err )
@@ -141,12 +107,12 @@ func TestList(t *testing.T) {
141107 }
142108}
143109
144- func TestListAdmins (t * testing.T ) {
110+ func TestList (t * testing.T ) {
145111 t .Parallel ()
146112
147- expected := []string {
148- "test" ,
149- " foo" ,
113+ expected := []api. UserPermResponse {
114+ { ID : 1 , Codename : "use_app" , Uniqueid : "autotest-app" , Username : "foo" } ,
115+ { ID : 2 , Codename : "use_cert" , Uniqueid : "autotest-cert-1" , Username : " foo"} ,
150116 }
151117
152118 handler := fakeHTTPServer {}
@@ -158,7 +124,7 @@ func TestListAdmins(t *testing.T) {
158124 t .Fatal (err )
159125 }
160126
161- actual , _ , err := ListAdmins (drycc , 100 )
127+ actual , _ , err := List (drycc , "" , 300 )
162128
163129 if err != nil {
164130 t .Fatal (err )
@@ -169,7 +135,7 @@ func TestListAdmins(t *testing.T) {
169135 }
170136}
171137
172- func TestNew (t * testing.T ) {
138+ func TestCreate (t * testing.T ) {
173139 t .Parallel ()
174140
175141 handler := fakeHTTPServer {}
@@ -181,24 +147,7 @@ func TestNew(t *testing.T) {
181147 t .Fatal (err )
182148 }
183149
184- if err = New (drycc , "example-go" , "foo" ); err != nil {
185- t .Fatal (err )
186- }
187- }
188-
189- func TestNewAdmin (t * testing.T ) {
190- t .Parallel ()
191-
192- handler := fakeHTTPServer {}
193- server := httptest .NewServer (handler )
194- defer server .Close ()
195-
196- drycc , err := drycc .New (false , server .URL , "abc" )
197- if err != nil {
198- t .Fatal (err )
199- }
200-
201- if err = NewAdmin (drycc , "test" ); err != nil {
150+ if err = Create (drycc , "use_app" , "autotest" , "foo" ); err != nil {
202151 t .Fatal (err )
203152 }
204153}
@@ -215,24 +164,7 @@ func TestDelete(t *testing.T) {
215164 t .Fatal (err )
216165 }
217166
218- if err = Delete (drycc , "example-go" , "foo" ); err != nil {
219- t .Fatal (err )
220- }
221- }
222-
223- func TestDeleteAdmin (t * testing.T ) {
224- t .Parallel ()
225-
226- handler := fakeHTTPServer {}
227- server := httptest .NewServer (handler )
228- defer server .Close ()
229-
230- drycc , err := drycc .New (false , server .URL , "abc" )
231- if err != nil {
232- t .Fatal (err )
233- }
234-
235- if err = DeleteAdmin (drycc , "test" ); err != nil {
167+ if err = Delete (drycc , "1" ); err != nil {
236168 t .Fatal (err )
237169 }
238170}
0 commit comments