github.com/greenpau/go-identity@v1.1.6/api_key_test.go (about) 1 // Copyright 2020 Paul Greenberg greenpau@outlook.com 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package identity 16 17 import ( 18 "fmt" 19 "github.com/greenpau/go-identity/internal/tests" 20 "github.com/greenpau/go-identity/pkg/errors" 21 "github.com/greenpau/go-identity/pkg/requests" 22 "testing" 23 ) 24 25 func TestNewAPIKey(t *testing.T) { 26 testcases := []struct { 27 name string 28 req *requests.Request 29 want map[string]interface{} 30 shouldErr bool 31 err error 32 }{ 33 { 34 name: "test api key", 35 req: &requests.Request{ 36 Key: requests.Key{ 37 Usage: "api", 38 Comment: "jsmith-api-key", 39 Payload: GetRandomStringFromRange(72, 96), 40 }, 41 }, 42 want: map[string]interface{}{ 43 "usage": "api", 44 "comment": "jsmith-api-key", 45 "disabled": false, 46 }, 47 }, 48 { 49 name: "test disabled api key", 50 req: &requests.Request{ 51 Key: requests.Key{ 52 Usage: "api", 53 Comment: "jsmith-api-key", 54 Disabled: true, 55 Payload: GetRandomStringFromRange(72, 96), 56 }, 57 }, 58 want: map[string]interface{}{ 59 "usage": "api", 60 "comment": "jsmith-api-key", 61 "disabled": true, 62 }, 63 }, 64 { 65 name: "test api key with empty payload", 66 req: &requests.Request{ 67 Key: requests.Key{ 68 Usage: "api", 69 Comment: "jsmith-api-key", 70 Disabled: true, 71 }, 72 }, 73 shouldErr: true, 74 err: errors.ErrAPIKeyPayloadEmpty, 75 }, 76 { 77 name: "test api key with empty payload", 78 req: &requests.Request{ 79 Key: requests.Key{ 80 Usage: "api", 81 Comment: "jsmith-api-key", 82 Disabled: true, 83 }, 84 }, 85 shouldErr: true, 86 err: errors.ErrAPIKeyPayloadEmpty, 87 }, 88 { 89 name: "test api key with empty usage", 90 req: &requests.Request{ 91 Key: requests.Key{ 92 Comment: "jsmith-api-key", 93 Payload: GetRandomStringFromRange(72, 96), 94 Disabled: true, 95 }, 96 }, 97 shouldErr: true, 98 err: errors.ErrAPIKeyUsageEmpty, 99 }, 100 { 101 name: "test api key with unsupported usage", 102 req: &requests.Request{ 103 Key: requests.Key{ 104 Usage: "foo", 105 Comment: "jsmith-api-key", 106 Payload: GetRandomStringFromRange(72, 96), 107 Disabled: true, 108 }, 109 }, 110 shouldErr: true, 111 err: errors.ErrAPIKeyUsageUnsupported.WithArgs("foo"), 112 }, 113 { 114 name: "test api key with empty comment", 115 req: &requests.Request{ 116 Key: requests.Key{ 117 Usage: "api", 118 Payload: GetRandomStringFromRange(72, 96), 119 Disabled: true, 120 }, 121 }, 122 shouldErr: true, 123 err: errors.ErrAPIKeyCommentEmpty, 124 }, 125 } 126 127 for _, tc := range testcases { 128 t.Run(tc.name, func(t *testing.T) { 129 msgs := []string{fmt.Sprintf("test name: %s", tc.name)} 130 if tc.req.Key.Payload != "" { 131 tc.req.Response.Payload = tc.req.Key.Payload 132 hk, _ := NewPassword(tc.req.Key.Payload) 133 tc.req.Key.Payload = hk.Hash 134 } 135 key, err := NewAPIKey(tc.req) 136 if tests.EvalErrWithLog(t, err, "new api key", tc.shouldErr, tc.err, msgs) { 137 return 138 } 139 140 got := make(map[string]interface{}) 141 got["usage"] = key.Usage 142 got["comment"] = key.Comment 143 got["disabled"] = key.Disabled 144 145 tests.EvalObjectsWithLog(t, "eval", tc.want, got, msgs) 146 key.Disable() 147 148 bundle := NewAPIKeyBundle() 149 bundle.Add(key) 150 bundle.Get() 151 bundle.Size() 152 }) 153 } 154 }