github.com/SUSE/skuba@v1.4.17/pkg/skuba/actions/auth/auth_test.go (about)

     1  /*
     2   * Copyright (c) 2019 SUSE LLC.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   *
    16   */
    17  
    18  package auth
    19  
    20  import (
    21  	"reflect"
    22  	"strings"
    23  	"testing"
    24  )
    25  
    26  func Test_processSingleConnector(t *testing.T) {
    27  	tests := []struct {
    28  		name              string
    29  		body              string
    30  		expectedConnector connector
    31  	}{
    32  		{
    33  			name: "single connector",
    34  			body: `
    35  			<!DOCTYPE html>
    36  			<html>
    37  				<head>
    38  				<title>SUSE CaaS Platform</title>
    39  				</head>
    40  				<body class="theme-body">
    41  				<div class="theme-navbar">
    42  					<div class="theme-navbar__logo-wrap">
    43  						<img class="theme-navbar__logo" src="https://10.86.4.17:32000/theme/logo.png">
    44  					</div>
    45  				</div>
    46  				<div class="dex-container">
    47  				<div class="theme-panel">
    48  				<h2 class="theme-heading">Log in to Your Account</h2>
    49  				<form method="post" action="/auth/ldap000?req=yfzclhtr4twoqqvixmdt75goe">
    50  				<div class="theme-form-row">
    51  					<div class="theme-form-label">
    52  						<label for="userid">Email Address</label>
    53  					</div>
    54  					<input tabindex="1" required id="login" name="login" type="text" class="theme-form-input" placeholder="email address"  autofocus />
    55  				</div>
    56  				<div class="theme-form-row">
    57  					<div class="theme-form-label">
    58  						<label for="password">Password</label>
    59  					</div>
    60  					<input tabindex="2" required id="password" name="password" type="password" class="theme-form-input" placeholder="password" />
    61  				</div>		
    62  				<button tabindex="3" id="submit-login" type="submit" class="dex-btn theme-btn--primary">Login</button>
    63  				</form>	
    64  				</body>
    65  			</html>
    66  			`,
    67  			expectedConnector: connector{
    68  				id:  "ldap000",
    69  				url: "/auth/ldap000?req=yfzclhtr4twoqqvixmdt75goe",
    70  			},
    71  		},
    72  	}
    73  
    74  	for _, tt := range tests {
    75  		tt := tt
    76  		t.Run(tt.name, func(t *testing.T) {
    77  			gotConnectors := processSingleConnector(strings.NewReader(tt.body))
    78  
    79  			if !reflect.DeepEqual(gotConnectors, tt.expectedConnector) {
    80  				t.Errorf("got %v, want %v", gotConnectors, tt.expectedConnector)
    81  				return
    82  			}
    83  		})
    84  	}
    85  }
    86  
    87  func Test_processMultipleConnectors(t *testing.T) {
    88  	tests := []struct {
    89  		name               string
    90  		body               string
    91  		expectedConnectors []connector
    92  	}{
    93  		{
    94  			name: "multiple connectors",
    95  			body: `
    96  				<!DOCTYPE html>
    97  				<html>
    98  					<head>
    99  					<title>SUSE CaaS Platform</title>
   100  					</head>
   101  					<body class="theme-body">
   102  					<div class="dex-container">
   103  						<div class="theme-panel">
   104  							<h2 class="theme-heading">Log in to SUSE CaaS Platform</h2>
   105  							<div>
   106  							<div class="theme-form-row">
   107  								<a href="/auth/local?req=ft6cvb6b4om3y7cbe3ncfw6mf" target="_self">
   108  								<button class="dex-btn theme-btn-provider">
   109  									<span class="dex-btn-icon dex-btn-icon--local"></span>
   110  									<span class="dex-btn-text">Log in with Email</span>
   111  								</button>
   112  								</a>
   113  							</div>
   114  							<div class="theme-form-row">
   115  								<a href="/auth/ldap?req=ft6cvb6b4om3y7cbe3ncfw6mf" target="_self">
   116  								<button class="dex-btn theme-btn-provider">
   117  									<span class="dex-btn-icon dex-btn-icon--ldap"></span>
   118  									<span class="dex-btn-text">Log in with openLDAP</span>
   119  								</button>
   120  								</a>
   121  							</div>
   122  						</div>
   123  					</div>
   124  					</body>
   125  				</html>
   126  				`,
   127  			expectedConnectors: []connector{
   128  				{
   129  					id:  "local",
   130  					url: "/auth/local?req=ft6cvb6b4om3y7cbe3ncfw6mf",
   131  				},
   132  				{
   133  					id:  "ldap",
   134  					url: "/auth/ldap?req=ft6cvb6b4om3y7cbe3ncfw6mf",
   135  				},
   136  			},
   137  		},
   138  		{
   139  			name: "no connector found",
   140  			body: `
   141  				<!DOCTYPE html>
   142  				<html>
   143  					<head>
   144  					<title>SUSE CaaS Platform</title>
   145  					</head>
   146  					<body class="theme-body">
   147  					<div class="dex-container">
   148  						<div class="theme-panel">
   149  							<h2 class="theme-heading">Log in to SUSE CaaS Platform</h2>
   150  							<div>
   151  						</div>
   152  					</div>
   153  					</body>
   154  				</html>
   155  				`,
   156  			expectedConnectors: []connector{},
   157  		},
   158  	}
   159  
   160  	for _, tt := range tests {
   161  		tt := tt
   162  		t.Run(tt.name, func(t *testing.T) {
   163  			gotConnectors := processMultipleConnectors(strings.NewReader(tt.body))
   164  
   165  			if !reflect.DeepEqual(gotConnectors, tt.expectedConnectors) {
   166  				t.Errorf("got %v, want %v", gotConnectors, tt.expectedConnectors)
   167  				return
   168  			}
   169  		})
   170  	}
   171  }
   172  
   173  func Example_printConnectors() {
   174  	c := []connector{
   175  		{id: "local", url: "/auth/local?req=ft6cvb6b4om3y7cbe3ncfw6mf"},
   176  		{id: "ldap", url: "/auth/ldap?req=ft6cvb6b4om3y7cbe3ncfw6mf"},
   177  	}
   178  	printConnectors(c)
   179  
   180  	// Output:
   181  	// Available authentication connector IDs are:
   182  	//   local
   183  	//   ldap
   184  }