-
Notifications
You must be signed in to change notification settings - Fork 76
/
api_test.go
131 lines (90 loc) · 3.96 KB
/
api_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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"testing"
"github.com/furkansenharputlu/f-license/lcs"
"github.com/furkansenharputlu/f-license/storage"
"github.com/stretchr/testify/assert"
)
func TestGenerateLicense(t *testing.T) {
defer storage.LicenseHandler.DropDatabase()
path := "/admin/licenses"
l := sampleLicense()
//l.Alg="RS256"
resp := tr.Run(t, &TestCase{Method: http.MethodPost, Path: path, Data: l, BodyMatch: `"id":.*"token":"ey.*"`})
resBytes, _ := ioutil.ReadAll(resp.Body)
var resMap map[string]string
_ = json.Unmarshal(resBytes, &resMap)
tr.Run(t, &TestCase{Method: http.MethodPost, Path: path, Data: l,
BodyMatch: "there is already such license with ID: " + resMap["id"]})
}
func TestGetLicense(t *testing.T) {
defer storage.LicenseHandler.DropDatabase()
path := "/admin/licenses"
l := sampleLicense(func(l *lcs.License) {
l.Headers["alg"] = "HS512"
})
resp := tr.Run(t, &TestCase{Method: http.MethodPost, Path: path, Data: l, BodyMatch: `"id":.*"token":"ey.*"`})
resBytes, _ := ioutil.ReadAll(resp.Body)
var resMap map[string]string
_ = json.Unmarshal(resBytes, &resMap)
expectedID := resMap["id"]
expectedToken := resMap["token"]
getPath := "/admin/licenses/" + expectedID
resp = tr.Run(t, &TestCase{Method: http.MethodGet, Path: getPath})
resBytes, _ = ioutil.ReadAll(resp.Body)
var retLicense lcs.License
_ = json.Unmarshal(resBytes, &retLicense)
assert.Equal(t, l.Headers, retLicense.Headers)
assert.Equal(t, l.Claims, retLicense.Claims)
assert.Equal(t, l.Active, retLicense.Active)
assert.Equal(t, expectedID, retLicense.ID.Hex())
assert.Equal(t, expectedToken, retLicense.Token)
}
func TestVerifyLicense(t *testing.T) {
defer storage.LicenseHandler.DropDatabase()
path := "/admin/licenses"
resp := tr.Run(t, &TestCase{Method: http.MethodPost, Path: path, Data: sampleLicense(), BodyMatch: `"id":.*"token":"ey.*"`})
resBytes, _ := ioutil.ReadAll(resp.Body)
var resMap map[string]string
_ = json.Unmarshal(resBytes, &resMap)
verifyPath := "/license/verify"
formParams := map[string]string{
"token": resMap["token"],
}
tr.Run(t, &TestCase{Method: http.MethodPost, Path: verifyPath, FormParams: formParams, BodyMatch: `"valid":true`})
}
func TestDeleteLicense(t *testing.T) {
defer storage.LicenseHandler.DropDatabase()
path := "/admin/licenses"
l := sampleLicense()
resp := tr.Run(t, &TestCase{Method: http.MethodPost, Path: path, Data: l, BodyMatch: `"id":.*"token":"ey.*"`})
resBytes, _ := ioutil.ReadAll(resp.Body)
var resMap map[string]string
_ = json.Unmarshal(resBytes, &resMap)
expectedID := resMap["id"]
tr.Run(t, &TestCase{Method: http.MethodPost, Path: path, Data: l,
BodyMatch: "there is already such license with ID: " + expectedID})
deletePath := fmt.Sprintf("/admin/licenses/%s/delete", expectedID)
tr.Run(t, &TestCase{Method: http.MethodDelete, Path: deletePath, BodyMatch: "License successfully deleted"})
tr.Run(t, &TestCase{Method: http.MethodPost, Path: path, Data: l, BodyMatch: `"id":.*"token":"ey.*"`})
}
func TestChangeLicenseActiveness(t *testing.T) {
defer storage.LicenseHandler.DropDatabase()
path := "/admin/licenses"
l := sampleLicense()
resp := tr.Run(t, &TestCase{Method: http.MethodPost, Path: path, Data: l, BodyMatch: `"id":.*"token":"ey.*"`})
resBytes, _ := ioutil.ReadAll(resp.Body)
var resMap map[string]string
_ = json.Unmarshal(resBytes, &resMap)
licenseID := resMap["id"]
inactivatePath := fmt.Sprintf("/admin/licenses/%s/inactivate", licenseID)
activatePath := fmt.Sprintf("/admin/licenses/%s/activate", licenseID)
tr.Run(t, &TestCase{Method: http.MethodPut, Path: inactivatePath, BodyMatch: `{"message":"Inactivated"}`})
tr.Run(t, &TestCase{Method: http.MethodPut, Path: inactivatePath, BodyMatch: `{"error":"already inactive"}`})
tr.Run(t, &TestCase{Method: http.MethodPut, Path: activatePath, BodyMatch: `{"message":"Activated"}`})
tr.Run(t, &TestCase{Method: http.MethodPut, Path: activatePath, BodyMatch: `{"error":"already active"}`})
}