github.com/greenpau/go-authcrunch@v1.1.4/pkg/authn/ui/ui_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 ui
    16  
    17  import (
    18  	"bytes"
    19  	"strings"
    20  	"testing"
    21  )
    22  
    23  func TestNewFactory(t *testing.T) {
    24  	t.Log("Creating UI factory")
    25  	f := NewFactory()
    26  	f.Title = "Authentication"
    27  	f.LogoURL = "/images/logo.png"
    28  	f.LogoDescription = "Authentication Portal"
    29  	officeLink := Link{
    30  		Title: "Office 365",
    31  		Link:  "https://office.com/",
    32  		Style: "fa-windows",
    33  	}
    34  	f.PublicLinks = append(f.PublicLinks, officeLink)
    35  	f.PrivateLinks = append(f.PrivateLinks, Link{
    36  		Title: "Prometheus",
    37  		Link:  "/prometheus",
    38  	})
    39  	f.PrivateLinks = append(f.PrivateLinks, Link{
    40  		Title: "Alertmanager",
    41  		Link:  "/alertmanager",
    42  	})
    43  	f.ActionEndpoint = "/auth/login"
    44  
    45  	t.Log("Adding a built-in template")
    46  	if err := f.AddBuiltinTemplate("basic/login"); err != nil {
    47  		t.Fatalf("Expected success, but got error: %s, %v", err, f.Templates)
    48  	}
    49  
    50  	t.Log("Adding a template from file system")
    51  	if err := f.AddTemplate("login", "../../../assets/portal/templates/basic/login.template"); err != nil {
    52  		t.Fatalf("Expected success, but got error: %s, %v", err, f.Templates)
    53  	}
    54  
    55  	loginRealm := make(map[string]string)
    56  	loginRealm["realm"] = "local"
    57  	loginRealm["label"] = strings.ToTitle("Local")
    58  	loginRealm["default"] = "yes"
    59  
    60  	var loginRealms []map[string]string
    61  	loginRealms = append(loginRealms, loginRealm)
    62  
    63  	loginOptions := make(map[string]interface{})
    64  	loginOptions["form_required"] = "yes"
    65  	loginOptions["realm_dropdown_required"] = "no"
    66  	loginOptions["identity_required"] = "yes"
    67  	loginOptions["realms"] = loginRealms
    68  	loginOptions["default_realm"] = "local"
    69  	loginOptions["authenticators"] = []map[string]interface{}{
    70  		map[string]interface{}{
    71  			"background_color":          "#324960",
    72  			"class_name":                "las la-key la-2x",
    73  			"color":                     "white",
    74  			"password_recovery_enabled": "y",
    75  			"realm":                     "local",
    76  			"text":                      "LOCAL",
    77  			"text_color":                "#37474f",
    78  		},
    79  	}
    80  
    81  	uiOptions := make(map[string]interface{})
    82  	uiOptions["custom_css_required"] = "no"
    83  	uiOptions["custom_js_required"] = "no"
    84  
    85  	t.Log("Rendering templates")
    86  	args := f.GetArgs()
    87  	args.Data["login_options"] = loginOptions
    88  	args.Data["ui_options"] = uiOptions
    89  
    90  	var t1, t2 *bytes.Buffer
    91  	var err error
    92  	if t1, err = f.Render("basic/login", args); err != nil {
    93  		t.Fatalf("Expected success, but got error: %s", err)
    94  	}
    95  
    96  	args = f.GetArgs()
    97  	args.Data["login_options"] = loginOptions
    98  	args.Data["ui_options"] = uiOptions
    99  	if t2, err = f.Render("login", args); err != nil {
   100  		t.Fatalf("Expected success, but got error: %s", err)
   101  	}
   102  	if strings.TrimSpace(t1.String()) != strings.TrimSpace(t2.String()) {
   103  		t.Fatalf("Expected templates to match, but got mismatch: %d (basic/login) vs. %d (login)", t1.Len(), t2.Len())
   104  	}
   105  
   106  }
   107  
   108  func TestAddBuiltinTemplates(t *testing.T) {
   109  	var expError string
   110  	t.Logf("Creating UI factory")
   111  	f := NewFactory()
   112  
   113  	t.Logf("Adding templates")
   114  	if err := f.AddBuiltinTemplates(); err != nil {
   115  		t.Fatal(err)
   116  	}
   117  
   118  	if err := f.AddBuiltinTemplate("saml"); err != nil {
   119  		expError = "built-in template saml does not exists"
   120  		if err.Error() != expError {
   121  			t.Fatalf("Mismatch between errors: %s (received) vs. %s (expected)", err.Error(), expError)
   122  		}
   123  	} else {
   124  		t.Fatalf("Expected an error, but got success")
   125  	}
   126  
   127  	t.Logf("Purging templates")
   128  	f.DeleteTemplates()
   129  
   130  	t.Logf("Re-adding templates")
   131  	if err := f.AddBuiltinTemplate("basic/login"); err != nil {
   132  		t.Fatalf("Expected success, but got error: %s", err)
   133  	}
   134  
   135  	t.Logf("Purging templates")
   136  	f.DeleteTemplates()
   137  
   138  	t.Logf("Re-adding templates")
   139  	if err := f.AddBuiltinTemplate("basic/login"); err != nil {
   140  		t.Fatalf("Expected success, but got error: %s", err)
   141  	}
   142  
   143  	t.Logf("Purging templates")
   144  	f.DeleteTemplates()
   145  
   146  	t.Logf("Re-adding templates")
   147  	if err := f.AddBuiltinTemplate("basic/portal"); err != nil {
   148  		t.Fatalf("Expected success, but got error: %s", err)
   149  	}
   150  }