github.com/greenpau/go-authcrunch@v1.1.4/pkg/identity/api_key_test.go (about) 1 // Copyright 2022 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 "testing" 20 21 "github.com/greenpau/go-authcrunch/internal/tests" 22 "github.com/greenpau/go-authcrunch/pkg/errors" 23 "github.com/greenpau/go-authcrunch/pkg/requests" 24 "github.com/greenpau/go-authcrunch/pkg/util" 25 ) 26 27 func TestNewAPIKey(t *testing.T) { 28 testcases := []struct { 29 name string 30 req *requests.Request 31 want map[string]interface{} 32 shouldErr bool 33 err error 34 }{ 35 { 36 name: "test api key", 37 req: &requests.Request{ 38 Key: requests.Key{ 39 Usage: "api", 40 Comment: "jsmith-api-key", 41 Payload: util.GetRandomStringFromRange(54, 72), 42 }, 43 }, 44 want: map[string]interface{}{ 45 "usage": "api", 46 "comment": "jsmith-api-key", 47 "disabled": false, 48 }, 49 }, 50 { 51 name: "test disabled api key", 52 req: &requests.Request{ 53 Key: requests.Key{ 54 Usage: "api", 55 Comment: "jsmith-api-key", 56 Disabled: true, 57 Payload: util.GetRandomStringFromRange(54, 72), 58 }, 59 }, 60 want: map[string]interface{}{ 61 "usage": "api", 62 "comment": "jsmith-api-key", 63 "disabled": true, 64 }, 65 }, 66 { 67 name: "test api key with empty payload", 68 req: &requests.Request{ 69 Key: requests.Key{ 70 Usage: "api", 71 Comment: "jsmith-api-key", 72 Disabled: true, 73 }, 74 }, 75 shouldErr: true, 76 err: errors.ErrAPIKeyPayloadEmpty, 77 }, 78 { 79 name: "test api key with empty payload", 80 req: &requests.Request{ 81 Key: requests.Key{ 82 Usage: "api", 83 Comment: "jsmith-api-key", 84 Disabled: true, 85 }, 86 }, 87 shouldErr: true, 88 err: errors.ErrAPIKeyPayloadEmpty, 89 }, 90 { 91 name: "test api key with empty usage", 92 req: &requests.Request{ 93 Key: requests.Key{ 94 Comment: "jsmith-api-key", 95 Payload: util.GetRandomStringFromRange(54, 72), 96 Disabled: true, 97 }, 98 }, 99 shouldErr: true, 100 err: errors.ErrAPIKeyUsageEmpty, 101 }, 102 { 103 name: "test api key with unsupported usage", 104 req: &requests.Request{ 105 Key: requests.Key{ 106 Usage: "foo", 107 Comment: "jsmith-api-key", 108 Payload: util.GetRandomStringFromRange(54, 72), 109 Disabled: true, 110 }, 111 }, 112 shouldErr: true, 113 err: errors.ErrAPIKeyUsageUnsupported.WithArgs("foo"), 114 }, 115 { 116 name: "test api key with empty comment", 117 req: &requests.Request{ 118 Key: requests.Key{ 119 Usage: "api", 120 Payload: util.GetRandomStringFromRange(54, 72), 121 Disabled: true, 122 }, 123 }, 124 shouldErr: true, 125 err: errors.ErrAPIKeyCommentEmpty, 126 }, 127 } 128 129 for _, tc := range testcases { 130 t.Run(tc.name, func(t *testing.T) { 131 msgs := []string{fmt.Sprintf("test name: %s", tc.name)} 132 if tc.req.Key.Payload != "" { 133 tc.req.Response.Payload = tc.req.Key.Payload 134 hk, err := NewPassword(tc.req.Key.Payload) 135 if err != nil { 136 t.Fatalf("unexpected password generation error: %s", err) 137 } 138 tc.req.Key.Payload = hk.Hash 139 } 140 key, err := NewAPIKey(tc.req) 141 if tests.EvalErrWithLog(t, err, "new api key", tc.shouldErr, tc.err, msgs) { 142 return 143 } 144 145 got := make(map[string]interface{}) 146 got["usage"] = key.Usage 147 got["comment"] = key.Comment 148 got["disabled"] = key.Disabled 149 150 tests.EvalObjectsWithLog(t, "eval", tc.want, got, msgs) 151 key.Disable() 152 153 bundle := NewAPIKeyBundle() 154 bundle.Add(key) 155 bundle.Get() 156 bundle.Size() 157 }) 158 } 159 }