-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathbasic_test.go
104 lines (92 loc) · 2.71 KB
/
basic_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
package main
import (
"encoding/json"
"fmt"
"os"
"strings"
"testing"
"github.com/dgraph-io/dgraph/testutil"
"github.com/dgraph-io/dgraph/x"
"github.com/stretchr/testify/require"
)
var adminEndpoint string
func makeRequest(t *testing.T, token *testutil.HttpToken,
params testutil.GraphQLParams) *testutil.GraphQLResponse {
resp := testutil.MakeGQLRequestWithAccessJwt(t, ¶ms, token.AccessJwt)
if len(resp.Errors) == 0 || !strings.Contains(resp.Errors.Error(), "Token is expired") {
return resp
}
var err error
newtoken, err := testutil.HttpLogin(&testutil.LoginParams{
Endpoint: adminEndpoint,
UserID: token.UserId,
Passwd: token.Password,
RefreshJwt: token.RefreshToken,
})
require.NoError(t, err)
token.AccessJwt = newtoken.AccessJwt
token.RefreshToken = newtoken.RefreshToken
return testutil.MakeGQLRequestWithAccessJwt(t, ¶ms, token.AccessJwt)
}
func createNamespace(t *testing.T, token *testutil.HttpToken) uint64 {
createNs := `
mutation{
createNamespace(input: {namespaceId: 1}){
namespaceId
message
}
}`
params := testutil.GraphQLParams{
Query: createNs,
}
resp := makeRequest(t, token, params)
var result struct {
CreateNamespace struct {
NamespaceId int `json:"namespaceId"`
Message string `json:"message"`
}
}
require.NoError(t, json.Unmarshal(resp.Data, &result))
require.Contains(t, result.CreateNamespace.Message, "Created namespace successfully")
return uint64(result.CreateNamespace.NamespaceId)
}
func deleteNamespace(t *testing.T, token *testutil.HttpToken, nsID uint64) {
deleteReq := `mutation deleteNamespace($namespaceId: Int!) {
deleteNamespace(input: {namespaceId: $namespaceId}){
namespaceId
message
}
}`
params := testutil.GraphQLParams{
Query: deleteReq,
Variables: map[string]interface{}{
"namespaceId": nsID,
},
}
resp := makeRequest(t, token, params)
var result struct {
DeleteNamespace struct {
NamespaceId int `json:"namespaceId"`
Message string `json:"message"`
}
}
require.NoError(t, json.Unmarshal(resp.Data, &result))
require.Equal(t, int(nsID), result.DeleteNamespace.NamespaceId)
require.Contains(t, result.DeleteNamespace.Message, "Deleted namespace successfully")
}
func TestCreateDelete(t *testing.T) {
token, err := testutil.HttpLogin(&testutil.LoginParams{
Endpoint: adminEndpoint,
UserID: x.GrootId,
Passwd: "password",
})
require.NoError(t, err, "login failed")
ns := createNamespace(t, token)
require.Equal(t, 1, int(ns))
deleteNamespace(t, token, 1)
}
func TestMain(m *testing.M) {
adminEndpoint = "http://" + testutil.SockAddrHttp + "/admin"
fmt.Printf("Using adminEndpoint : %s for multy-tenancy test.\n", adminEndpoint)
os.Exit(m.Run())
}