github.com/freiheit-com/kuberpult@v1.24.2-0.20240328135542-315d5630abe6/pkg/auth/auth_test.go (about)

     1  /*This file is part of kuberpult.
     2  
     3  Kuberpult is free software: you can redistribute it and/or modify
     4  it under the terms of the Expat(MIT) License as published by
     5  the Free Software Foundation.
     6  
     7  Kuberpult is distributed in the hope that it will be useful,
     8  but WITHOUT ANY WARRANTY; without even the implied warranty of
     9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    10  MIT License for more details.
    11  
    12  You should have received a copy of the MIT License
    13  along with kuberpult. If not, see <https://directory.fsf.org/wiki/License:Expat>.
    14  
    15  Copyright 2023 freiheit.com*/
    16  
    17  package auth
    18  
    19  import (
    20  	"context"
    21  	"testing"
    22  )
    23  
    24  func TestToFromContext(t *testing.T) {
    25  	tcs := []struct {
    26  		Name         string
    27  		Author       User
    28  		ExpectedUser User
    29  	}{
    30  		{
    31  			Name: "User is fully specified",
    32  			Author: User{
    33  				Email: "new@test.com",
    34  				Name:  "New",
    35  			},
    36  			ExpectedUser: User{
    37  				Email: "new@test.com",
    38  				Name:  "New",
    39  			},
    40  		},
    41  		{
    42  			Name: "Name is not specified",
    43  			Author: User{
    44  				Email: "new@test.com",
    45  			},
    46  			ExpectedUser: User{
    47  				Email: "new@test.com",
    48  				Name:  "",
    49  			},
    50  		},
    51  		{
    52  			Name: "Email is not specified",
    53  			Author: User{
    54  				Name: "my name",
    55  			},
    56  			ExpectedUser: User{
    57  				Email: "",
    58  				Name:  "my name",
    59  			},
    60  		},
    61  		{
    62  			Name:   "User is not specified",
    63  			Author: User{},
    64  			ExpectedUser: User{
    65  				Email: "",
    66  				Name:  "",
    67  			},
    68  		},
    69  	}
    70  
    71  	for _, tc := range tcs {
    72  		tc := tc
    73  		t.Run(tc.Name, func(t *testing.T) {
    74  			t.Parallel()
    75  			ctx := WriteUserToContext(context.Background(), tc.Author)
    76  			u, err := ReadUserFromContext(ctx)
    77  			if err != nil {
    78  				t.Fatalf("Unexpected error: %#v \n", err)
    79  			}
    80  			if u.Email != tc.ExpectedUser.Email {
    81  				t.Fatalf("Unexpected Email was extracted from context.\nexpected: %#v \nrecieved: %#v \n", tc.ExpectedUser.Email, u.Email)
    82  			}
    83  			if u.Name != tc.ExpectedUser.Name {
    84  				t.Fatalf("Unexpected Name was extracted from context.\nexpected: %#v \nrecieved: %#v \n", tc.ExpectedUser.Name, u.Name)
    85  			}
    86  		})
    87  	}
    88  }
    89  func TestGetUserOrDefault(t *testing.T) {
    90  	tcs := []struct {
    91  		Name         string
    92  		OptionalUser *User
    93  		DefaultUser  User
    94  		ExpectedUser User
    95  	}{
    96  		{
    97  			Name: "User is fully specified",
    98  			OptionalUser: &User{
    99  				Email: "a1",
   100  				Name:  "a2",
   101  			},
   102  			DefaultUser: User{
   103  				Email: "d1",
   104  				Name:  "d2",
   105  			},
   106  			ExpectedUser: User{
   107  				Email: "a1",
   108  				Name:  "a2",
   109  			},
   110  		},
   111  		{
   112  			Name: "User name empty",
   113  			OptionalUser: &User{
   114  				Email: "a1",
   115  				Name:  "",
   116  			},
   117  			DefaultUser: User{
   118  				Email: "d1",
   119  				Name:  "d2",
   120  			},
   121  			ExpectedUser: User{
   122  				Email: "a1",
   123  				Name:  "a1",
   124  			},
   125  		},
   126  		{
   127  			Name:         "User is empty",
   128  			OptionalUser: nil,
   129  			DefaultUser: User{
   130  				Email: "d1",
   131  				Name:  "d2",
   132  			},
   133  			ExpectedUser: User{
   134  				Email: "d1",
   135  				Name:  "d2",
   136  			},
   137  		},
   138  	}
   139  
   140  	for _, tc := range tcs {
   141  		tc := tc
   142  		t.Run(tc.Name, func(t *testing.T) {
   143  			t.Parallel()
   144  			actualUser := GetUserOrDefault(tc.OptionalUser, tc.DefaultUser)
   145  			if actualUser.Email != tc.ExpectedUser.Email {
   146  				t.Fatalf("Unexpected Email was extracted from context.\nexpected: %#v \nrecieved: %#v \n", tc.ExpectedUser.Email, actualUser.Email)
   147  			}
   148  			if actualUser.Name != tc.ExpectedUser.Name {
   149  				t.Fatalf("Unexpected Email was extracted from context.\nexpected: %#v \nrecieved: %#v \n", tc.ExpectedUser.Name, actualUser.Name)
   150  			}
   151  		})
   152  	}
   153  }