-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from tsaron/ref/tsaron-anansi
Finally refactor for anansi
- Loading branch information
Showing
8 changed files
with
271 additions
and
202 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
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,57 +1,138 @@ | ||
package jwt | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
"syreclabs.com/go/faker" | ||
) | ||
|
||
type jwtStruct struct { | ||
Name string `json:"name"` | ||
} | ||
|
||
func TestEncode(t *testing.T) { | ||
jwt := jwtStruct{Name: "Olakunkle"} | ||
token, err := Encode("claims", []byte("mysecret"), time.Minute, jwt) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if token == "" { | ||
t.Error("Expected EncodeJWT to generate a token") | ||
} | ||
} | ||
|
||
func TestDecode(t *testing.T) { | ||
secret := []byte("test-secret") | ||
|
||
t.Run("should decode token generated by encode", func(t *testing.T) { | ||
jwt := jwtStruct{Name: "testName"} | ||
token, err := Encode("claims", secret, time.Minute, jwt) | ||
t.Run("should encode a map", func(t *testing.T) { | ||
payload := map[string]interface{}{"name": faker.Name().FirstName()} | ||
token, err := Encode(secret, time.Minute, payload) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
var loaded jwtStruct | ||
if err := Decode("claims", secret, []byte(token), &loaded); err != nil { | ||
if token == "" { | ||
t.Error("Expected a token, got an empty string") | ||
} | ||
|
||
parsed, err := Decode(secret, []byte(token)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if loaded.Name != "testName" { | ||
t.Errorf("Expected Name to be %s, got %s", "Olakunle", loaded.Name) | ||
if parsed["name"] != payload["name"] { | ||
t.Errorf("Expected the parsed name to be %s, got %s", payload["name"], parsed["name"]) | ||
} | ||
}) | ||
|
||
t.Run("should decode token generated externally", func(t *testing.T) { | ||
// don't use this token it would fail. | ||
token := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwiY2xhaW0iOnsibmFtZSI6InRva2VuIn0sImV4cCI6MTU5OTUyODEwNSwiaWF0IjoxNTE2MjM5MDIyfQ.-u5OhBHdq7L_wBlI2cBjstOXyhRS2m27bx1tLN869so" | ||
|
||
var loaded jwtStruct | ||
if err := Decode("claim", secret, []byte(token), &loaded); err != nil { | ||
t.Run("should fail with an error", func(t *testing.T) { | ||
payload := map[string]interface{}{"name": faker.Name().FirstName()} | ||
token, err := Encode(secret, time.Second, payload) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if loaded.Name != "token" { | ||
t.Errorf("Expected Name to be %s, got %s", "Olakunle", loaded.Name) | ||
time.Sleep(time.Second * 2) | ||
|
||
_, err = Decode(secret, []byte(token)) | ||
if err == nil { | ||
t.Fatal("Expected Decode to fail with non-nil error") | ||
} | ||
|
||
if err != ErrJWTExpired { | ||
t.Errorf("Expected Decode to fail with ErrJWTExpired, failed with %v", err) | ||
} | ||
}) | ||
} | ||
|
||
func TestEncodeStruct(t *testing.T) { | ||
secret := []byte("test-secret") | ||
payload := jwtStruct{faker.Name().FirstName()} | ||
|
||
token, err := EncodeStruct(secret, time.Second, payload) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if token == "" { | ||
t.Error("Expected a token, got an empty string") | ||
} | ||
|
||
var parsed jwtStruct | ||
err = DecodeStruct(secret, []byte(token), &parsed) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if parsed.Name != payload.Name { | ||
t.Errorf("Expected the parsed name to be %s, got %s", payload.Name, parsed.Name) | ||
} | ||
} | ||
|
||
func TestEncodeEmbedded(t *testing.T) { | ||
secret := []byte("test-secret") | ||
payload := jwtStruct{faker.Name().FirstName()} | ||
|
||
token, err := EncodeEmbedded(secret, time.Second, payload) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if token == "" { | ||
t.Error("Expected a token, got an empty string") | ||
} | ||
|
||
parsed, err := Decode(secret, []byte(token)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if parsed["claim"] == nil { | ||
t.Fatal("Expected the claim to be non-nil") | ||
} | ||
|
||
data, ok := parsed["claim"].(map[string]interface{}) | ||
fmt.Println(data) | ||
if !ok { | ||
t.Fatalf("Expected claim to be a map of string to string, got %T", parsed["claim"]) | ||
} | ||
|
||
if data["name"] != payload.Name { | ||
t.Errorf("Expected the parsed name to be %s, got %s", payload.Name, data["name"]) | ||
} | ||
} | ||
|
||
func TestDecodeEmbedded(t *testing.T) { | ||
secret := []byte("test-secret") | ||
payload := jwtStruct{faker.Name().FirstName()} | ||
|
||
token, err := EncodeEmbedded(secret, time.Second, payload) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if token == "" { | ||
t.Error("Expected a token, got an empty string") | ||
} | ||
|
||
var parsed jwtStruct | ||
err = DecodeEmbedded(secret, []byte(token), &parsed) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if parsed.Name != payload.Name { | ||
t.Errorf("Expected the parsed name to be %s, got %s", payload.Name, parsed.Name) | ||
} | ||
} |
Oops, something went wrong.