Skip to content

Commit

Permalink
Add test.JSONMarshal utility to force an error check in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Tit Petric committed Aug 16, 2023
1 parent fd6489d commit c0f5a97
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 21 deletions.
42 changes: 21 additions & 21 deletions gateway/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,14 +230,14 @@ func TestKeyHandler(t *testing.T) {

// Access right not specified
masterKey := CreateStandardSession()
masterKeyJSON, _ := json.Marshal(masterKey)
masterKeyJSON := test.MarshalJSON(t)(masterKey)
//TestTykMakeHTTPRequest
// with access
withAccess := CreateStandardSession()
withAccess.AccessRights = map[string]user.AccessDefinition{"test": {
APIID: "test", Versions: []string{"v1"},
}}
withAccessJSON, _ := json.Marshal(withAccess)
withAccessJSON := test.MarshalJSON(t)(withAccess)

// with policy
ts.Gw.policiesMu.Lock()
Expand All @@ -252,12 +252,12 @@ func TestKeyHandler(t *testing.T) {
}
ts.Gw.policiesMu.Unlock()
withPolicy := CreateStandardSession()
withoutPolicyJSON, _ := json.Marshal(withPolicy)
withoutPolicyJSON := test.MarshalJSON(t)(withPolicy)

withPolicy.ApplyPolicies = []string{
"abc_policy",
}
withPolicyJSON, _ := json.Marshal(withPolicy)
withPolicyJSON := test.MarshalJSON(t)(withPolicy)

// with invalid policy
withBadPolicy := CreateStandardSession()
Expand All @@ -267,13 +267,13 @@ func TestKeyHandler(t *testing.T) {
withBadPolicy.ApplyPolicies = []string{
"xyz_policy",
}
withBadPolicyJSON, _ := json.Marshal(withBadPolicy)
withBadPolicyJSON := test.MarshalJSON(t)(withBadPolicy)

withUnknownAPI := CreateStandardSession()
withUnknownAPI.AccessRights = map[string]user.AccessDefinition{"unknown": {
APIID: "unknown", Versions: []string{"v1"},
}}
withUnknownAPIJSON, _ := json.Marshal(withUnknownAPI)
withUnknownAPIJSON := test.MarshalJSON(t)(withUnknownAPI)

t.Run("Create key", func(t *testing.T) {
_, _ = ts.Run(t, []test.TestCase{
Expand Down Expand Up @@ -468,7 +468,7 @@ func TestKeyHandler_UpdateKey(t *testing.T) {

t.Run("Add policy not enforcing acl", func(t *testing.T) {
session.ApplyPolicies = append(session.ApplyPolicies, pID2)
sessionData, _ := json.Marshal(session)
sessionData := test.MarshalJSON(t)(session)
path := fmt.Sprintf("/tyk/keys/%s", key)

_, _ = ts.Run(t, []test.TestCase{
Expand All @@ -484,7 +484,7 @@ func TestKeyHandler_UpdateKey(t *testing.T) {

t.Run("Remove policy not enforcing acl", func(t *testing.T) {
session.ApplyPolicies = []string{}
sessionData, _ := json.Marshal(session)
sessionData := test.MarshalJSON(t)(session)
path := fmt.Sprintf("/tyk/keys/%s", key)

_, _ = ts.Run(t, []test.TestCase{
Expand All @@ -499,7 +499,7 @@ func TestKeyHandler_UpdateKey(t *testing.T) {

t.Run("Tags on key level", func(t *testing.T) {
assertTags := func(session *user.SessionState, expected []string) {
sessionData, _ := json.Marshal(session)
sessionData := test.MarshalJSON(t)(session)
path := fmt.Sprintf("/tyk/keys/%s", key)

_, _ = ts.Run(t, []test.TestCase{
Expand Down Expand Up @@ -540,7 +540,7 @@ func TestKeyHandler_UpdateKey(t *testing.T) {

t.Run("MetaData on key level", func(t *testing.T) {
assertMetaData := func(session *user.SessionState, expected map[string]interface{}) {
sessionData, _ := json.Marshal(session)
sessionData := test.MarshalJSON(t)(session)
path := fmt.Sprintf("/tyk/keys/%s", key)

_, _ = ts.Run(t, []test.TestCase{
Expand Down Expand Up @@ -719,7 +719,7 @@ func TestUpdateKeyWithCert(t *testing.T) {
})

session.Certificate = newCertID
sessionData, _ := json.Marshal(session)
sessionData := test.MarshalJSON(t)(session)

path := fmt.Sprintf("/tyk/keys/%s", key)
_, _ = ts.Run(t, []test.TestCase{
Expand All @@ -742,7 +742,7 @@ func TestUpdateKeyWithCert(t *testing.T) {

// attempt to set an empty cert
session.Certificate = ""
sessionData, _ := json.Marshal(session)
sessionData := test.MarshalJSON(t)(session)

path := fmt.Sprintf("/tyk/keys/%s", key)
_, _ = ts.Run(t, []test.TestCase{
Expand All @@ -764,7 +764,7 @@ func TestUpdateKeyWithCert(t *testing.T) {
})

session.Certificate = "invalid-cert-id"
sessionData, _ := json.Marshal(session)
sessionData := test.MarshalJSON(t)(session)

path := fmt.Sprintf("/tyk/keys/%s", key)
_, _ = ts.Run(t, []test.TestCase{
Expand Down Expand Up @@ -841,7 +841,7 @@ func TestKeyHandler_CheckKeysNotDuplicateOnUpdate(t *testing.T) {
t.Error("Failed to create key, ensure security settings are correct:" + err.Error())
}

requestByte, _ := json.Marshal(session)
requestByte := test.MarshalJSON(t)(session)
r := httptest.NewRequest(http.MethodPut, "/tyk/keys/"+keyName, bytes.NewReader(requestByte))
ts.Gw.handleAddOrUpdate(keyName, r, tc.HashKeys)

Expand Down Expand Up @@ -1027,7 +1027,7 @@ func (ts *Test) testHashKeyHandlerHelper(t *testing.T, expectedHashSize int) {
withAccess.AccessRights = map[string]user.AccessDefinition{"test": {
APIID: "test", Versions: []string{"v1"},
}}
withAccessJSON, _ := json.Marshal(withAccess)
withAccessJSON := test.MarshalJSON(t)(withAccess)

myKey := "my_key_id"
myKeyHash := storage.HashKey(ts.Gw.generateToken("default", myKey), ts.Gw.GetConfig().HashKeys)
Expand Down Expand Up @@ -1201,7 +1201,7 @@ func TestHashKeyListingDisabled(t *testing.T) {
withAccess.AccessRights = map[string]user.AccessDefinition{"test": {
APIID: "test", Versions: []string{"v1"},
}}
withAccessJSON, _ := json.Marshal(withAccess)
withAccessJSON := test.MarshalJSON(t)(withAccess)

myKey := "my_key_id"
myKeyHash := storage.HashKey(ts.Gw.generateToken("default", myKey), ts.Gw.GetConfig().HashKeys)
Expand Down Expand Up @@ -1319,7 +1319,7 @@ func TestKeyHandler_HashingDisabled(t *testing.T) {
withAccess.AccessRights = map[string]user.AccessDefinition{"test": {
APIID: "test", Versions: []string{"v1"},
}}
withAccessJSON, _ := json.Marshal(withAccess)
withAccessJSON := test.MarshalJSON(t)(withAccess)

myKeyID := "my_key_id"
token := ts.Gw.generateToken("default", myKeyID)
Expand Down Expand Up @@ -1460,7 +1460,7 @@ func TestGetOAuthClients(t *testing.T) {
APIID: "test",
ClientSecret: "secret",
}
validOauthRequest, _ := json.Marshal(oauthRequest)
validOauthRequest := test.MarshalJSON(t)(oauthRequest)

ts.Run(t, []test.TestCase{
{Path: "/tyk/oauth/clients/unknown", AdminAuth: true, Code: 404},
Expand Down Expand Up @@ -1565,7 +1565,7 @@ func TestCreateOAuthClient(t *testing.T) {

for testName, testData := range tests {
t.Run(testName, func(t *testing.T) {
requestData, _ := json.Marshal(testData.req)
requestData := test.MarshalJSON(t)(testData.req)
_, _ = ts.Run(
t,
test.TestCase{
Expand Down Expand Up @@ -1690,7 +1690,7 @@ func TestUpdateOauthClientHandler(t *testing.T) {

for testName, testData := range tests {
t.Run(testName, func(t *testing.T) {
requestData, _ := json.Marshal(testData.req)
requestData := test.MarshalJSON(t)(testData.req)
testCase := test.TestCase{
Method: http.MethodPut,
Path: "/tyk/oauth/clients/test/12345",
Expand Down Expand Up @@ -2036,7 +2036,7 @@ func TestRotateClientSecretHandler(t *testing.T) {

for testName, testData := range tests {
t.Run(testName, func(t *testing.T) {
requestData, _ := json.Marshal(testData.req)
requestData := test.MarshalJSON(t)(testData.req)
testCase := test.TestCase{
Method: http.MethodPut,
Path: "/tyk/oauth/clients/test/12345/rotate",
Expand Down
18 changes: 18 additions & 0 deletions test/json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package test

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/assert"
)

// MarshalJSON returns a closure returning the marshalled json
// while asserting no error occured during marshalling.
func MarshalJSON(t *testing.T) func(interface{}) []byte {
return func(in interface{}) []byte {
b, err := json.Marshal(in)
assert.NoError(t, err)
return b
}
}
14 changes: 14 additions & 0 deletions test/json_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package test

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestJSON(t *testing.T) {
value := "foo"
marshal := JSON(t)

Check failure on line 11 in test/json_test.go

View workflow job for this annotation

GitHub Actions / Go 1.19.x Redis 5

undefined: JSON

Check failure on line 11 in test/json_test.go

View workflow job for this annotation

GitHub Actions / Go 1.19.x Redis 5

undeclared name: JSON
out := marshal(value)
assert.Equal(t, []byte(`"foo"`), out)
}

0 comments on commit c0f5a97

Please sign in to comment.