Skip to content

Commit

Permalink
Added tests to improve coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
ThatsMrTalbot committed Jun 15, 2016
1 parent 9759c7b commit 6e99166
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
8 changes: 8 additions & 0 deletions prototoken.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ func GenerateToken(object proto.Message, key PrivateKey) (*pb.Token, error) {
return nil, errors.Wrap(err, "Value could not be marshaled")
}

if key == nil {
return nil, errors.New("Cannot sign using nil key")
}

signature, err := key.Generate(value.Value)
return &pb.Token{
Value: value,
Expand Down Expand Up @@ -79,6 +83,10 @@ func ValidateString(data string, key PublicKey, result proto.Message) (*pb.Token

// ValidateToken validates token object
func ValidateToken(token *pb.Token, key PublicKey, result proto.Message) error {
if key == nil {
return errors.New("Cannot verify using nil key")
}

if err := key.Validate(token.Value.Value, token.Signature); err != nil {
return errors.Wrap(err, "Token could not be validated")
}
Expand Down
72 changes: 72 additions & 0 deletions prototoken_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,30 @@ func TestToken(t *testing.T) {
So(err, ShouldNotBeNil)
})
})

Convey("When a token is validated with a nil key", func() {
result := new(pb.ExampleToken)
err := ValidateToken(token, nil, result)

Convey("Then the token should not be valid", func() {
So(err, ShouldNotBeNil)
})
})
})

Convey("Given a nill key", t, func() {
value := &pb.ExampleToken{
Some: "abc",
Example: 123,
Values: true,
}
Convey("When a token is generated", func() {
_, err := GenerateToken(value, nil)

Convey("Then an error should be returned", func() {
So(err, ShouldNotBeNil)
})
})
})
}

Expand Down Expand Up @@ -76,6 +100,30 @@ func TestTokenString(t *testing.T) {
So(err, ShouldNotBeNil)
})
})

Convey("When a token is validated with a nil key", func() {
result := new(pb.ExampleToken)
_, err := ValidateString(token, nil, result)

Convey("Then the token should not be valid", func() {
So(err, ShouldNotBeNil)
})
})
})

Convey("Given a nill key", t, func() {
value := &pb.ExampleToken{
Some: "abc",
Example: 123,
Values: true,
}
Convey("When a string token is generated", func() {
_, err := GenerateString(value, nil)

Convey("Then an error should be returned", func() {
So(err, ShouldNotBeNil)
})
})
})
}

Expand Down Expand Up @@ -111,6 +159,30 @@ func TestTokenBytes(t *testing.T) {
So(err, ShouldNotBeNil)
})
})

Convey("When a token is validated with a nil key", func() {
result := new(pb.ExampleToken)
_, err := ValidateBytes(token, nil, result)

Convey("Then the token should not be valid", func() {
So(err, ShouldNotBeNil)
})
})
})

Convey("Given a nill key", t, func() {
value := &pb.ExampleToken{
Some: "abc",
Example: 123,
Values: true,
}
Convey("When a byte token is generated", func() {
_, err := GenerateBytes(value, nil)

Convey("Then an error should be returned", func() {
So(err, ShouldNotBeNil)
})
})
})
}

Expand Down

0 comments on commit 6e99166

Please sign in to comment.