github.com/greenpau/go-authcrunch@v1.1.4/pkg/sso/request_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 sso 16 17 import ( 18 "net/http" 19 "net/url" 20 "testing" 21 22 "github.com/google/go-cmp/cmp" 23 "github.com/greenpau/go-authcrunch/pkg/errors" 24 ) 25 26 func TestParseRequestURL(t *testing.T) { 27 testcases := []struct { 28 name string 29 input *http.Request 30 want *Request 31 shouldErr bool 32 err error 33 }{ 34 { 35 name: "test metadata request", 36 input: &http.Request{ 37 Method: "GET", 38 URL: &url.URL{ 39 Path: "/apps/sso/aws/metadata.xml", 40 }, 41 }, 42 want: &Request{ 43 ProviderName: "aws", 44 Kind: MetadataRequest, 45 }, 46 }, 47 { 48 name: "test role selection request", 49 input: &http.Request{ 50 Method: "GET", 51 URL: &url.URL{ 52 Path: "/apps/sso/aws", 53 }, 54 }, 55 want: &Request{ 56 ProviderName: "aws", 57 Kind: MenuRequest, 58 }, 59 }, 60 { 61 name: "test assume role request", 62 input: &http.Request{ 63 Method: "GET", 64 URL: &url.URL{ 65 Path: "/apps/sso/aws/assume/123456789012/Administrator", 66 }, 67 }, 68 want: &Request{ 69 ProviderName: "aws", 70 Kind: AssumeRoleRequest, 71 Params: "123456789012/Administrator", 72 }, 73 }, 74 { 75 name: "test malformed assume role request", 76 input: &http.Request{ 77 Method: "GET", 78 URL: &url.URL{ 79 Path: "/apps/sso/aws/assume/", 80 }, 81 }, 82 shouldErr: true, 83 err: errors.ErrSingleSignOnProviderRequestMalformed, 84 }, 85 { 86 name: "test malformed request", 87 input: &http.Request{ 88 Method: "GET", 89 URL: &url.URL{ 90 Path: "foo/bar", 91 }, 92 }, 93 shouldErr: true, 94 err: errors.ErrSingleSignOnProviderRequestMalformed, 95 }, 96 { 97 name: "test malformed request without provider", 98 input: &http.Request{ 99 Method: "GET", 100 URL: &url.URL{ 101 Path: "/apps/sso/", 102 }, 103 }, 104 shouldErr: true, 105 err: errors.ErrSingleSignOnProviderRequestMalformed, 106 }, 107 { 108 name: "test malformed request with invalid params", 109 input: &http.Request{ 110 Method: "GET", 111 URL: &url.URL{ 112 Path: "/apps/sso/aws/foobar", 113 }, 114 }, 115 shouldErr: true, 116 err: errors.ErrSingleSignOnProviderRequestMalformed, 117 }, 118 } 119 for _, tc := range testcases { 120 t.Run(tc.name, func(t *testing.T) { 121 got, err := ParseRequestURL(tc.input) 122 if err != nil { 123 if !tc.shouldErr { 124 t.Fatalf("expected success, got: %v", err) 125 } 126 if diff := cmp.Diff(err.Error(), tc.err.Error()); diff != "" { 127 t.Fatalf("unexpected error: %v, want: %v", err, tc.err) 128 } 129 return 130 } 131 if tc.shouldErr { 132 t.Fatalf("unexpected success, want: %v", tc.err) 133 } 134 if diff := cmp.Diff(tc.want, got); diff != "" { 135 t.Errorf("ParseRequestURL() mismatch (-want +got):\n%s", diff) 136 } 137 }) 138 } 139 }