File: claims_test.go

package info (click to toggle)
golang-github-dgrijalva-jwt-go 3.2.0-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 344 kB
  • sloc: makefile: 5
file content (105 lines) | stat: -rw-r--r-- 2,070 bytes parent folder | download | duplicates (2)
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package jwt

import (
	"testing"
)

// Test StandardClaims instances with an audience value populated in a string, []string and []interface{}
var audienceValue = "Aud"
var unmatchedAudienceValue = audienceValue + "Test"
var claimWithAudience = []StandardClaims{
	{
		audienceValue,
		123123,
		"Id",
		12312,
		"Issuer",
		12312,
		"Subject",
	},
	{
		[]string{audienceValue, unmatchedAudienceValue},
		123123,
		"Id",
		12312,
		"Issuer",
		12312,
		"Subject",
	},
	{
		[]interface{}{audienceValue, unmatchedAudienceValue},
		123123,
		"Id",
		12312,
		"Issuer",
		12312,
		"Subject",
	},
}

// Test StandardClaims instances with no aduences within empty []string and []interface{} collections.
var claimWithoutAudience = []StandardClaims{
	{
		[]string{},
		123123,
		"Id",
		12312,
		"Issuer",
		12312,
		"Subject",
	},
	{
		[]interface{}{},
		123123,
		"Id",
		12312,
		"Issuer",
		12312,
		"Subject",
	},
}

func TestExtractAudienceWithAudienceValues(t *testing.T) {
	for _, data := range claimWithAudience {
		var aud = ExtractAudience(&data)
		if len(aud) == 0 || aud[0] != audienceValue {
			t.Errorf("The audience value was not extracted properly")
		}
	}
}

func TestExtractAudience_WithoutAudienceValues(t *testing.T) {
	for _, data := range claimWithoutAudience {
		var aud = ExtractAudience(&data)
		if len(aud) != 0 {
			t.Errorf("An audience value should not have been extracted")
		}
	}
}

var audWithValues = [][]string{
	[]string{audienceValue},
	[]string{"Aud1", "Aud2", audienceValue},
}

var audWithLackingOriginalValue = [][]string{
	[]string{},
	[]string{audienceValue + "1"},
	[]string{"Aud1", "Aud2", audienceValue + "1"},
}

func TestVerifyAud_ShouldVerifyExists(t *testing.T) {
	for _, data := range audWithValues {
		if !verifyAud(data, audienceValue, true) {
			t.Errorf("The audience value was not verified properly")
		}
	}
}

func TestVerifyAud_ShouldVerifyDoesNotExist(t *testing.T) {
	for _, data := range audWithValues {
		if !verifyAud(data, audienceValue, true) {
			t.Errorf("The audience value was not verified properly")
		}
	}
}