-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathns_test.go
73 lines (56 loc) · 2.63 KB
/
ns_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
package main
import (
"github.com/kataras/iris/httptest"
"github.com/muxiyun/Mae/model"
"testing"
"time"
)
func TestCreateAndDeleteNamespace(t *testing.T) {
e := httptest.New(t, newApp(), httptest.URL("http://127.0.0.1:8080"))
defer model.DB.RWdb.DropTableIfExists("users")
defer model.DB.RWdb.DropTableIfExists("casbin_rule")
//create a user and an admin
CreateUserForTest(e, "andrew", "123456", "3480437308@qq.com")
CreateAdminForTest(e, "andrewadmin", "123456", "admin@qq.com")
//test an anonymous to create test-ns-1
e.POST("/api/v1.0/ns/{ns}").WithPath("ns", "test-ns-1").
Expect().Status(httptest.StatusForbidden)
//test a user to create test-ns-2
andrew_token := GetTokenForTest(e, "andrew", "123456", 60*60)
e.POST("/api/v1.0/ns/{ns}").WithPath("ns", "test-ns-2").
WithBasicAuth(andrew_token, "").Expect().Body().Contains("OK")
//test a admin to create test-ns-3
andrewadmin_token := GetTokenForTest(e, "andrewadmin", "123456", 60*60)
e.POST("/api/v1.0/ns/{ns}").WithPath("ns", "test-ns-3").
WithBasicAuth(andrewadmin_token, "").Expect().Body().Contains("OK")
//test an anonymous to delete test-ns-2
e.DELETE("/api/v1.0/ns/{ns}").WithPath("ns", "test-ns-2").Expect().
Status(httptest.StatusForbidden)
//test a normal user to delete test-ns-2
e.DELETE("/api/v1.0/ns/{ns}").WithPath("ns", "test-ns-2").WithBasicAuth(andrew_token, "").
Expect().Status(httptest.StatusForbidden)
time.Sleep(5*time.Second)
// test an admin user to delete test-ns-2 and test-ns-3
e.DELETE("/api/v1.0/ns/{ns}").WithPath("ns", "test-ns-2").WithBasicAuth(andrewadmin_token, "").
Expect().Body().Contains("OK")
e.DELETE("/api/v1.0/ns/{ns}").WithPath("ns", "test-ns-3").WithBasicAuth(andrewadmin_token, "").
Expect().Body().Contains("OK")
}
func TestListNamespace(t *testing.T) {
e := httptest.New(t, newApp(), httptest.URL("http://127.0.0.1:8080"))
defer model.DB.RWdb.DropTableIfExists("users")
defer model.DB.RWdb.DropTableIfExists("casbin_rule")
//create a user and an admin
CreateUserForTest(e, "andrew", "123456", "3480437308@qq.com")
CreateAdminForTest(e, "andrewadmin", "123456", "admin@qq.com")
//test an anonymous to list namespaces
e.GET("/api/v1.0/ns").Expect().Status(httptest.StatusForbidden)
//test a user to list namespaces
andrew_token := GetTokenForTest(e, "andrew", "123456", 60*60)
e.GET("/api/v1.0/ns").WithBasicAuth(andrew_token, "").Expect().
Body().Contains("OK").NotContains("kube-system")
//test an admin to list namespaces
andrewadmin_token := GetTokenForTest(e, "andrewadmin", "123456", 60*60)
e.GET("/api/v1.0/ns").WithBasicAuth(andrewadmin_token, "").Expect().
Body().Contains("OK").Contains("kube-system")
}