github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/pkg/auth/converter_test.go (about) 1 package auth 2 3 import ( 4 "reflect" 5 "testing" 6 7 "github.com/kyma-incubator/compass/components/director/internal/model" 8 "github.com/kyma-incubator/compass/components/director/pkg/graphql" 9 "github.com/stretchr/testify/assert" 10 ) 11 12 var ( 13 authUsername = "user" 14 authPassword = "password" 15 authEndpoint = "url" 16 accessStrategy = "testAccessStrategy" 17 authMap = map[string][]string{ 18 "foo": {"bar", "baz"}, 19 "too": {"tar", "taz"}, 20 } 21 authMapSerialized = "{\"foo\":[\"bar\",\"baz\"],\"too\":[\"tar\",\"taz\"]}" 22 authHeaders = graphql.HTTPHeaders(authMap) 23 authHeadersSerialized = graphql.HTTPHeadersSerialized(authMapSerialized) 24 authParams = graphql.QueryParams(authMap) 25 authParamsSerialized = graphql.QueryParamsSerialized(authMapSerialized) 26 ) 27 28 func TestConverter_ToGraphQLInput(t *testing.T) { 29 // GIVEN 30 testCases := []struct { 31 Name string 32 Input *model.Auth 33 Expected *graphql.AuthInput 34 ExpectedErrorMsg string 35 }{ 36 { 37 Name: "All properties given", 38 Input: fixModelAuth(), 39 Expected: fixGQLAuthInput(), 40 }, 41 { 42 Name: "Empty", 43 Input: &model.Auth{}, 44 Expected: &graphql.AuthInput{Credential: &graphql.CredentialDataInput{}}, 45 }, 46 { 47 Name: "Nil", 48 Input: nil, 49 Expected: nil, 50 ExpectedErrorMsg: "Missing system auth", 51 }, 52 } 53 54 for _, testCase := range testCases { 55 t.Run(testCase.Name, func(t *testing.T) { 56 // WHEN 57 res, err := ToGraphQLInput(testCase.Input) 58 59 // then 60 if testCase.ExpectedErrorMsg != "" { 61 assert.Error(t, err) 62 assert.Contains(t, err.Error(), testCase.ExpectedErrorMsg) 63 } else { 64 assert.NoError(t, err) 65 reflect.DeepEqual(testCase.Expected, res) 66 } 67 }) 68 } 69 } 70 71 func TestConverter_ToModel(t *testing.T) { 72 // GIVEN 73 expectedAuthInputWithNilReqAuth := fixGQLAuthInput() 74 expectedAuthInputWithNilReqAuth.RequestAuth = nil 75 76 testCases := []struct { 77 Name string 78 Input *graphql.Auth 79 Expected *model.Auth 80 ExpectedErrorMsg string 81 }{ 82 { 83 Name: "All properties given", 84 Input: fixGqlAuth(), 85 Expected: fixModelAuth(), 86 }, 87 { 88 Name: "Empty", 89 Input: &graphql.Auth{}, 90 Expected: &model.Auth{}, 91 }, 92 { 93 Name: "Nil", 94 Input: nil, 95 Expected: nil, 96 ExpectedErrorMsg: "", 97 }, 98 } 99 100 for _, testCase := range testCases { 101 t.Run(testCase.Name, func(t *testing.T) { 102 // WHEN 103 res, err := ToModel(testCase.Input) 104 105 // then 106 if testCase.ExpectedErrorMsg != "" { 107 assert.Error(t, err) 108 assert.Contains(t, err.Error(), testCase.ExpectedErrorMsg) 109 } else { 110 assert.NoError(t, err) 111 reflect.DeepEqual(testCase.Expected, res) 112 } 113 }) 114 } 115 } 116 117 func fixModelAuth() *model.Auth { 118 return &model.Auth{ 119 Credential: model.CredentialData{ 120 Basic: &model.BasicCredentialData{ 121 Username: authUsername, 122 Password: authPassword, 123 }, 124 Oauth: nil, 125 }, 126 AccessStrategy: &accessStrategy, 127 AdditionalHeaders: authMap, 128 AdditionalQueryParams: authMap, 129 RequestAuth: &model.CredentialRequestAuth{ 130 Csrf: &model.CSRFTokenCredentialRequestAuth{ 131 TokenEndpointURL: authEndpoint, 132 Credential: model.CredentialData{ 133 Basic: &model.BasicCredentialData{ 134 Username: authUsername, 135 Password: authPassword, 136 }, 137 Oauth: nil, 138 }, 139 AdditionalHeaders: authMap, 140 AdditionalQueryParams: authMap, 141 }, 142 }, 143 } 144 } 145 146 func fixGqlAuth() *graphql.Auth { 147 return &graphql.Auth{ 148 Credential: graphql.BasicCredentialData{ 149 Username: authUsername, 150 Password: authPassword, 151 }, 152 AccessStrategy: &accessStrategy, 153 AdditionalHeaders: authHeaders, 154 AdditionalQueryParams: authParams, 155 RequestAuth: &graphql.CredentialRequestAuth{ 156 Csrf: &graphql.CSRFTokenCredentialRequestAuth{ 157 TokenEndpointURL: authEndpoint, 158 Credential: graphql.BasicCredentialData{ 159 Username: authUsername, 160 Password: authPassword, 161 }, 162 AdditionalHeaders: authHeaders, 163 AdditionalQueryParams: authParams, 164 }, 165 }, 166 } 167 } 168 169 func fixGQLAuthInput() *graphql.AuthInput { 170 return &graphql.AuthInput{ 171 Credential: &graphql.CredentialDataInput{ 172 Basic: &graphql.BasicCredentialDataInput{ 173 Username: authUsername, 174 Password: authPassword, 175 }, 176 Oauth: nil, 177 }, 178 AccessStrategy: &accessStrategy, 179 AdditionalHeaders: authHeaders, 180 AdditionalHeadersSerialized: &authHeadersSerialized, 181 AdditionalQueryParams: authParams, 182 AdditionalQueryParamsSerialized: &authParamsSerialized, 183 RequestAuth: &graphql.CredentialRequestAuthInput{ 184 Csrf: &graphql.CSRFTokenCredentialRequestAuthInput{ 185 TokenEndpointURL: authEndpoint, 186 Credential: &graphql.CredentialDataInput{ 187 Basic: &graphql.BasicCredentialDataInput{ 188 Username: authUsername, 189 Password: authPassword, 190 }, 191 Oauth: nil, 192 }, 193 AdditionalHeaders: authHeaders, 194 AdditionalHeadersSerialized: &authHeadersSerialized, 195 AdditionalQueryParams: authParams, 196 AdditionalQueryParamsSerialized: &authParamsSerialized, 197 }, 198 }, 199 } 200 }