[#283] pkg/session: Cover Token's Sign/Verify methods with unit test
Add `sessiontest.GenerateSigned` function which returns signed random token. Clarify that `sessiontest.Generate` returns an unsigned token. Use these functions to assert the correctness of `Sign` / `VerifySignature` methods. Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
parent
05e74d56db
commit
fb0b1ea108
2 changed files with 36 additions and 0 deletions
|
@ -66,3 +66,23 @@ func TestSessionTokenEncoding(t *testing.T) {
|
||||||
require.Equal(t, tok, tok2)
|
require.Equal(t, tok, tok2)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestToken_VerifySignature(t *testing.T) {
|
||||||
|
t.Run("nil", func(t *testing.T) {
|
||||||
|
var tok *session.Token
|
||||||
|
|
||||||
|
require.False(t, tok.VerifySignature())
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("unsigned", func(t *testing.T) {
|
||||||
|
tok := sessiontest.Generate()
|
||||||
|
|
||||||
|
require.False(t, tok.VerifySignature())
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("signed", func(t *testing.T) {
|
||||||
|
tok := sessiontest.GenerateSigned()
|
||||||
|
|
||||||
|
require.True(t, tok.VerifySignature())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
|
@ -11,6 +11,8 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// Generate returns random session.Token.
|
// Generate returns random session.Token.
|
||||||
|
//
|
||||||
|
// Resulting token is unsigned.
|
||||||
func Generate() *session.Token {
|
func Generate() *session.Token {
|
||||||
tok := session.NewToken()
|
tok := session.NewToken()
|
||||||
|
|
||||||
|
@ -33,3 +35,17 @@ func Generate() *session.Token {
|
||||||
|
|
||||||
return tok
|
return tok
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GenerateSigned returns signed random session.Token.
|
||||||
|
//
|
||||||
|
// Panics if token could not be signed (actually unexpected).
|
||||||
|
func GenerateSigned() *session.Token {
|
||||||
|
tok := Generate()
|
||||||
|
|
||||||
|
err := tok.Sign(test.DecodeKey(0))
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return tok
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue