Repository navigation
Expand file tree
/
Copy pathcontext_test.go
More file actions
72 lines (50 loc) · 1.9 KB
/
Copy pathcontext_test.go
File metadata and controls
72 lines (50 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright 2026 Hyperscale. All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
package security_test
import (
"context"
"testing"
"github.com/hyperscale-stack/security"
"github.com/stretchr/testify/assert"
)
func TestFromContextWithoutStoredValueReturnsAnonymous(t *testing.T) {
t.Parallel()
auth, ok := security.FromContext(context.Background())
assert.False(t, ok, "ok must be false when nothing was stored")
assert.Equal(t, security.Anonymous(), auth, "must fall back to Anonymous()")
assert.False(t, auth.IsAuthenticated())
}
func TestWithAuthenticationRoundtrip(t *testing.T) {
t.Parallel()
stored := newFakeAuth("alice", "ROLE_USER").withAuthenticated()
ctx := security.WithAuthentication(context.Background(), stored)
got, ok := security.FromContext(ctx)
assert.True(t, ok)
assert.Equal(t, stored, got)
assert.True(t, got.IsAuthenticated())
}
func TestWithAuthenticationNilClearsTheSlot(t *testing.T) {
t.Parallel()
stored := newFakeAuth("alice").withAuthenticated()
ctx := security.WithAuthentication(context.Background(), stored)
ctx = security.WithAuthentication(ctx, nil)
got, ok := security.FromContext(ctx)
assert.False(t, ok)
assert.Equal(t, security.Anonymous(), got)
// A typed nil reads back as absent too: callers never get a value that
// panics on its first method call.
got, ok = security.FromContext(security.WithAuthentication(context.Background(), typedNilAuth))
assert.False(t, ok)
assert.Equal(t, security.Anonymous(), got)
}
func TestWithAuthenticationOverwrites(t *testing.T) {
t.Parallel()
first := newFakeAuth("alice").withAuthenticated()
second := newFakeAuth("bob").withAuthenticated()
ctx := security.WithAuthentication(context.Background(), first)
ctx = security.WithAuthentication(ctx, second)
got, ok := security.FromContext(ctx)
assert.True(t, ok)
assert.Equal(t, second, got)
}