-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added tests for generic claims, subjects and some types
Added taglist type to all claims
- Loading branch information
Stephen Asbury
committed
Nov 8, 2018
1 parent
98f5dca
commit c29390f
Showing
7 changed files
with
210 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package jwt | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestNewGenericClaims(t *testing.T) { | ||
akp := createAccountNKey(t) | ||
apk := publicKey(akp, t) | ||
|
||
uc := NewGenericClaims(apk) | ||
uc.Expires = time.Now().Add(time.Duration(time.Hour)).UTC().Unix() | ||
uc.Name = "alberto" | ||
uc.Audience = "everyone" | ||
uc.NotBefore = time.Now().UTC().Unix() | ||
uc.Tags.Add("one") | ||
uc.Tags.Add("one") | ||
uc.Tags.Add("one") | ||
uc.Tags.Add("TWO") // should become lower case | ||
uc.Tags.Add("three") | ||
|
||
uJwt := encode(uc, akp, t) | ||
|
||
uc2, err := DecodeGeneric(uJwt) | ||
if err != nil { | ||
t.Fatal("failed to decode", err) | ||
} | ||
|
||
AssertEquals(uc.String(), uc2.String(), t) | ||
AssertEquals(uc.Name, uc2.Name, t) | ||
AssertEquals(uc.Audience, uc2.Audience, t) | ||
AssertEquals(uc.Expires, uc2.Expires, t) | ||
AssertEquals(uc.NotBefore, uc2.NotBefore, t) | ||
AssertEquals(uc.Subject, uc2.Subject, t) | ||
|
||
AssertEquals(3, len(uc2.Tags), t) | ||
AssertEquals(true, uc2.Tags.Contains("two"), t) | ||
AssertEquals("one", uc2.Tags[0], t) | ||
AssertEquals("two", uc2.Tags[1], t) | ||
AssertEquals("three", uc2.Tags[2], t) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package jwt | ||
|
||
import "testing" | ||
|
||
func TestSubjectValid(t *testing.T) { | ||
var s Subject | ||
|
||
s = "" | ||
err := s.Valid() | ||
if err == nil { | ||
t.Fatalf("Empty string is not a valid subjects") | ||
} | ||
|
||
s = "has spaces" | ||
err = s.Valid() | ||
if err == nil { | ||
t.Fatalf("Subjects cannot contain spaces") | ||
} | ||
|
||
s = "has.spa ces.and.tokens" | ||
err = s.Valid() | ||
if err == nil { | ||
t.Fatalf("Subjects cannot have spaces") | ||
} | ||
|
||
s = "one" | ||
err = s.Valid() | ||
if err != nil { | ||
t.Fatalf("%s is a valid subject", s) | ||
} | ||
|
||
s = "one.two.three" | ||
err = s.Valid() | ||
if err != nil { | ||
t.Fatalf("%s is a valid subject", s) | ||
} | ||
} | ||
|
||
func TestHasWildCards(t *testing.T) { | ||
var s Subject | ||
|
||
s = "one" | ||
AssertEquals(false, s.HasWildCards(), t) | ||
|
||
s = "one.two.three" | ||
AssertEquals(false, s.HasWildCards(), t) | ||
|
||
s = "*" | ||
AssertEquals(true, s.HasWildCards(), t) | ||
|
||
s = "one.*.three" | ||
AssertEquals(true, s.HasWildCards(), t) | ||
|
||
s = "*.two.three" | ||
AssertEquals(true, s.HasWildCards(), t) | ||
|
||
s = "one.two.*" | ||
AssertEquals(true, s.HasWildCards(), t) | ||
|
||
s = "one.>" | ||
AssertEquals(true, s.HasWildCards(), t) | ||
|
||
s = "one.two.>" | ||
AssertEquals(true, s.HasWildCards(), t) | ||
|
||
s = ">" | ||
AssertEquals(true, s.HasWildCards(), t) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,55 @@ | ||
package jwt | ||
|
||
import "testing" | ||
|
||
func TestTagList(t *testing.T) { | ||
tags := TagList{} | ||
|
||
tags.Add("one") | ||
|
||
AssertEquals(true, tags.Contains("one"), t) | ||
AssertEquals(true, tags.Contains("ONE"), t) | ||
AssertEquals("one", tags[0], t) | ||
|
||
tags.Add("TWO") | ||
|
||
AssertEquals(true, tags.Contains("two"), t) | ||
AssertEquals(true, tags.Contains("TWO"), t) | ||
AssertEquals("two", tags[1], t) | ||
|
||
tags.Remove("ONE") | ||
AssertEquals("two", tags[0], t) | ||
AssertEquals(false, tags.Contains("one"), t) | ||
AssertEquals(false, tags.Contains("ONE"), t) | ||
} | ||
|
||
func TestStringList(t *testing.T) { | ||
slist := StringList{} | ||
|
||
slist.Add("one") | ||
|
||
AssertEquals(true, slist.Contains("one"), t) | ||
AssertEquals(false, slist.Contains("ONE"), t) | ||
AssertEquals("one", slist[0], t) | ||
|
||
slist.Add("TWO") | ||
|
||
AssertEquals(false, slist.Contains("two"), t) | ||
AssertEquals(true, slist.Contains("TWO"), t) | ||
AssertEquals("TWO", slist[1], t) | ||
|
||
slist.Remove("ONE") | ||
AssertEquals("one", slist[0], t) | ||
AssertEquals(true, slist.Contains("one"), t) | ||
AssertEquals(false, slist.Contains("ONE"), t) | ||
|
||
slist.Add("ONE") | ||
AssertEquals(true, slist.Contains("one"), t) | ||
AssertEquals(true, slist.Contains("ONE"), t) | ||
AssertEquals(3, len(slist), t) | ||
|
||
slist.Remove("one") | ||
AssertEquals("TWO", slist[0], t) | ||
AssertEquals(false, slist.Contains("one"), t) | ||
AssertEquals(true, slist.Contains("ONE"), t) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters