github.com/greenpau/go-authcrunch@v1.1.4/pkg/authz/handlers/redirect_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 handlers
    16  
    17  import (
    18  	"github.com/greenpau/go-authcrunch/internal/tests"
    19  	"github.com/greenpau/go-authcrunch/pkg/requests"
    20  	"net/http"
    21  	"testing"
    22  )
    23  
    24  type customResponseWriter struct {
    25  	body       []byte
    26  	statusCode int
    27  	header     http.Header
    28  }
    29  
    30  func buildCustomResponseWriter() *customResponseWriter {
    31  	return &customResponseWriter{
    32  		header: http.Header{},
    33  	}
    34  }
    35  
    36  func (w *customResponseWriter) Header() http.Header {
    37  	return w.header
    38  }
    39  
    40  func (w *customResponseWriter) Write(b []byte) (int, error) {
    41  	w.body = b
    42  	return 0, nil
    43  }
    44  
    45  func (w *customResponseWriter) WriteHeader(statusCode int) {
    46  	w.statusCode = statusCode
    47  }
    48  
    49  func TestRedirect(t *testing.T) {
    50  	var testcases = []struct {
    51  		name              string
    52  		expectedAuthURL   string
    53  		expectedSeparator string
    54  		testCase          requests.AuthorizationRequest
    55  	}{
    56  		{
    57  			name:              "returns a AuthUrl as it is",
    58  			expectedAuthURL:   "something.com",
    59  			expectedSeparator: "?",
    60  			testCase: requests.AuthorizationRequest{
    61  				Redirect: requests.RedirectResponse{
    62  					AuthURL:        "something.com",
    63  					QueryParameter: "redirect_url",
    64  				},
    65  			},
    66  		},
    67  		{
    68  			name:              "returns a AuthUrl with loginHint only",
    69  			expectedAuthURL:   "something.com?login_hint=fakemail%40byom.de",
    70  			expectedSeparator: "&",
    71  			testCase: requests.AuthorizationRequest{
    72  				Redirect: requests.RedirectResponse{
    73  					LoginHint:      "fakemail@byom.de",
    74  					AuthURL:        "something.com",
    75  					QueryParameter: "redirect_url",
    76  				},
    77  			},
    78  		},
    79  		{
    80  			name:              "returns a AuthUrl with additional scopes only",
    81  			expectedAuthURL:   "something.com?additional_scopes=scopeA+scopeB",
    82  			expectedSeparator: "&",
    83  			testCase: requests.AuthorizationRequest{
    84  				Redirect: requests.RedirectResponse{
    85  					AdditionalScopes: "scopeA scopeB",
    86  					AuthURL:          "something.com",
    87  					QueryParameter:   "redirect_url",
    88  				},
    89  			},
    90  		},
    91  		{
    92  			name:              "returns a AuthUrl with additional scopes and login hint",
    93  			expectedAuthURL:   "something.com?login_hint=fakemail%40byom.de&additional_scopes=scopeA+scopeB",
    94  			expectedSeparator: "&",
    95  			testCase: requests.AuthorizationRequest{
    96  				Redirect: requests.RedirectResponse{
    97  					LoginHint:        "fakemail@byom.de",
    98  					AdditionalScopes: "scopeA scopeB",
    99  					AuthURL:          "something.com",
   100  					QueryParameter:   "redirect_url",
   101  				},
   102  			},
   103  		},
   104  	}
   105  	for _, tc := range testcases {
   106  		t.Run(tc.name, func(t *testing.T) {
   107  			r, _ := http.NewRequest("GET", "https://foo.bar", nil)
   108  			configureRedirect(buildCustomResponseWriter(), r, &tc.testCase)
   109  			tests.EvalObjects(t, "check the expected AuthUrl", tc.expectedAuthURL, tc.testCase.Redirect.AuthURL)
   110  			tests.EvalObjects(t, "check the expected Separator", tc.expectedSeparator, tc.testCase.Redirect.Separator)
   111  		})
   112  	}
   113  }