Skip to content

Commit

Permalink
Added tests for generic claims, subjects and some types
Browse files Browse the repository at this point in the history
Added taglist type to all claims
  • Loading branch information
Stephen Asbury committed Nov 8, 2018
1 parent 98f5dca commit c29390f
Show file tree
Hide file tree
Showing 7 changed files with 210 additions and 6 deletions.
1 change: 1 addition & 0 deletions claims.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type ClaimsData struct {
Name string `json:"name,omitempty"`
NotBefore int64 `json:"nbf,omitempty"`
Subject string `json:"sub,omitempty"`
Tags TagList `json:"tags,omitempty"`
Type ClaimType `json:"type,omitempty"`
}

Expand Down
42 changes: 42 additions & 0 deletions genericclaims_test.go
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)
}
5 changes: 4 additions & 1 deletion subjects.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,8 @@ func (s Subject) HasWildCards() bool {
v := string(s)
return strings.HasSuffix(v, ".>") ||
strings.Contains(v, ".*.") ||
strings.HasSuffix(v, ".*")
strings.HasSuffix(v, ".*") ||
strings.HasPrefix(v, "*.") ||
v == "*" ||
v == ">"
}
68 changes: 68 additions & 0 deletions subjects_test.go
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)
}
40 changes: 38 additions & 2 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package jwt

import (
"fmt"
"strings"
)

type Account struct {
Expand Down Expand Up @@ -46,7 +47,7 @@ type Permissions struct {

type StringList []string

func (u *StringList) contains(p string) bool {
func (u *StringList) Contains(p string) bool {
for _, t := range *u {
if t == p {
return true
Expand All @@ -57,7 +58,7 @@ func (u *StringList) contains(p string) bool {

func (u *StringList) Add(p ...string) {
for _, v := range p {
if !u.contains(v) && v != "" {
if !u.Contains(v) && v != "" {
*u = append(*u, v)
}
}
Expand All @@ -75,6 +76,41 @@ func (u *StringList) Remove(p ...string) {
}
}

// TagList is a unique array of lower case strings
type TagList []string

func (u *TagList) Contains(p string) bool {
p = strings.ToLower(p)
for _, t := range *u {
if t == p {
return true
}
}
return false
}

func (u *TagList) Add(p ...string) {
for _, v := range p {
v = strings.ToLower(v)
if !u.Contains(v) && v != "" {
*u = append(*u, v)
}
}
}

func (u *TagList) Remove(p ...string) {
for _, v := range p {
v = strings.ToLower(v)
for i, t := range *u {
if t == v {
a := *u
*u = append(a[:i], a[i+1:]...)
break
}
}
}
}

func (u *Permissions) Valid() error {
return nil
}
Expand Down
54 changes: 54 additions & 0 deletions types_test.go
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)
}
6 changes: 3 additions & 3 deletions user_claims_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,15 @@ func TestSubjects(t *testing.T) {
if len(s) != 0 {
t.Fatalf("expected len 0")
}
if s.contains("a") {
if s.Contains("a") {
t.Fatalf("didn't expect 'a'")
}
s.Add("a")
if !s.contains("a") {
if !s.Contains("a") {
t.Fatalf("expected 'a'")
}
s.Remove("a")
if s.contains("a") {
if s.Contains("a") {
t.Fatalf("didn't expect 'a' after removing")
}
}

0 comments on commit c29390f

Please sign in to comment.