-
-
Notifications
You must be signed in to change notification settings - Fork 187
Expand file tree
/
Copy pathidentity_test.go
More file actions
84 lines (77 loc) · 2.14 KB
/
Copy pathidentity_test.go
File metadata and controls
84 lines (77 loc) · 2.14 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
73
74
75
76
77
78
79
80
81
82
83
84
package gateway
import (
"net/http"
"testing"
"github.com/shellhub-io/shellhub/pkg/api/authorizer"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestIdentityRoundTrip is what keeps [Identity.WriteTo] and [IdentityFrom] naming the same headers.
// A field added to one and forgotten in the other survives review and the compiler, and shows up
// only as an identity that silently loses part of itself between the authenticator and the handler.
func TestIdentityRoundTrip(t *testing.T) {
cases := []struct {
description string
identity Identity
}{
{
description: "a user token",
identity: Identity{
ID: "user-id",
Username: "username",
TenantID: "00000000-0000-4000-0000-000000000000",
Role: authorizer.RoleOwner,
},
},
{
description: "an api key",
identity: Identity{
TenantID: "00000000-0000-4000-0000-000000000000",
APIKey: "key",
Role: authorizer.RoleObserver,
},
},
{
description: "a device token",
identity: Identity{
DeviceUID: "device-uid",
TenantID: "00000000-0000-4000-0000-000000000000",
},
},
{
description: "an admin browsing the admin console",
identity: Identity{
Username: "username",
Role: authorizer.RoleOwner,
Admin: true,
},
},
}
for _, tc := range cases {
t.Run(tc.description, func(tt *testing.T) {
header := http.Header{}
tc.identity.WriteTo(header)
require.Equal(tt, tc.identity, IdentityFrom(header))
})
}
}
// TestIdentityActorDropsAuthorization pins what an actor deliberately is not. Role and admin decide
// what the caller may do, which the middleware owns; handing them to a handler invites it to make
// that decision a second time.
func TestIdentityActorDropsAuthorization(t *testing.T) {
identity := Identity{
ID: "user-id",
Username: "username",
TenantID: "00000000-0000-4000-0000-000000000000",
DeviceUID: "device-uid",
APIKey: "key",
Role: authorizer.RoleOwner,
Admin: true,
}
assert.Equal(t, Actor{
ID: "user-id",
Username: "username",
APIKey: "key",
DeviceUID: "device-uid",
}, identity.Actor())
}