github.com/google/go-safeweb@v0.0.0-20231219055052-64d8cfc90fbb/examples/sample-application/secure/auth/ctx_test.go (about)

     1  // Copyright 2022 Google LLC
     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  //	https://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 auth
    16  
    17  import (
    18  	"net/http/httptest"
    19  	"testing"
    20  
    21  	"github.com/google/go-safeweb/safehttp"
    22  )
    23  
    24  func TestPutSessionAction(t *testing.T) {
    25  	req := safehttp.NewIncomingRequest(httptest.NewRequest(safehttp.MethodGet, "/spaghetti", nil))
    26  	ctx := req.Context()
    27  
    28  	putSessionAction(ctx, setSess)
    29  
    30  	if v := safehttp.FlightValues(ctx).Get(changeSessKey); v != setSess {
    31  		t.Errorf("putSessionAction(), got: %q, want: %q ", v, setSess)
    32  	}
    33  }
    34  
    35  func TestCtxSessionAction(t *testing.T) {
    36  	tests := []struct {
    37  		name  string
    38  		value interface{}
    39  		want  sessionAction
    40  	}{
    41  		{
    42  			name:  "basic session action set/get",
    43  			value: setSess,
    44  			want:  setSess,
    45  		},
    46  		{
    47  			name:  "no sessionAction value, return empty string",
    48  			value: "unexpected",
    49  			want:  "",
    50  		},
    51  	}
    52  	for _, tt := range tests {
    53  		t.Run(tt.name, func(t *testing.T) {
    54  			req := safehttp.NewIncomingRequest(httptest.NewRequest(safehttp.MethodGet, "/", nil))
    55  			ctx := req.Context()
    56  
    57  			safehttp.FlightValues(ctx).Put(changeSessKey, tt.value)
    58  
    59  			if got := ctxSessionAction(ctx); got != tt.want {
    60  				t.Errorf("ctxSessionAction() = %v, want %v", got, tt.want)
    61  			}
    62  		})
    63  	}
    64  }
    65  
    66  func TestPutUser(t *testing.T) {
    67  	req := safehttp.NewIncomingRequest(httptest.NewRequest(safehttp.MethodGet, "/spaghetti", nil))
    68  	ctx := req.Context()
    69  	want := "someoneName"
    70  
    71  	putUser(ctx, want)
    72  
    73  	if v := safehttp.FlightValues(ctx).Get(userKey); v != want {
    74  		t.Errorf("putUser(), got: %q, want: %q ", v, setSess)
    75  	}
    76  }
    77  
    78  func TestCtxUser(t *testing.T) {
    79  	tests := []struct {
    80  		name  string
    81  		value interface{}
    82  		want  string
    83  	}{
    84  		{
    85  			name:  "basic session user set/get",
    86  			value: "safeUser",
    87  			want:  "safeUser",
    88  		},
    89  		{
    90  			name:  "no string value, return empty string",
    91  			value: 42,
    92  			want:  "",
    93  		},
    94  	}
    95  	for _, tt := range tests {
    96  		t.Run(tt.name, func(t *testing.T) {
    97  			req := safehttp.NewIncomingRequest(httptest.NewRequest(safehttp.MethodGet, "/", nil))
    98  			ctx := req.Context()
    99  
   100  			safehttp.FlightValues(ctx).Put(userKey, tt.value)
   101  
   102  			if got := ctxUser(ctx); got != tt.want {
   103  				t.Errorf("ctxUser() = %v, want %v", got, tt.want)
   104  			}
   105  		})
   106  	}
   107  }