Skip to content

Commit

Permalink
Validate optional account username.
Browse files Browse the repository at this point in the history
  • Loading branch information
atomatt committed Oct 14, 2016
1 parent 9113d5b commit 0bf01a5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
5 changes: 4 additions & 1 deletion asserts/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@ func assembleAccount(assert assertionBase) (Assertion, error) {
return nil, err
}

// TODO: username is optional (atm) but if it's there it must be a string
_, err = checkOptionalString(assert.headers, "username")
if err != nil {
return nil, err
}

return &Account{
assertionBase: assert,
Expand Down
16 changes: 16 additions & 0 deletions asserts/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,21 @@ func (s *accountSuite) TestDecodeOK(c *C) {
c.Check(account.IsCertified(), Equals, true)
}

func (s *accountSuite) TestOptional(c *C) {
encoded := strings.Replace(accountExample, "TSLINE", s.tsLine, 1)

tests := []struct{ original, replacement string }{
{"username: nice\n", ""},
{"username: nice\n", "username: \n"},
}

for _, test := range tests {
valid := strings.Replace(encoded, test.original, test.replacement, 1)
_, err := asserts.Decode([]byte(valid))
c.Check(err, IsNil)
}
}

func (s *accountSuite) TestIsCertified(c *C) {
tests := []struct {
value string
Expand Down Expand Up @@ -105,6 +120,7 @@ func (s *accountSuite) TestDecodeInvalid(c *C) {
{"account-id: abc-123\n", "account-id: \n", `"account-id" header should not be empty`},
{"display-name: Nice User\n", "", `"display-name" header is mandatory`},
{"display-name: Nice User\n", "display-name: \n", `"display-name" header should not be empty`},
{"username: nice\n", "username:\n - foo\n - bar\n", `"username" header must be a string`},
{"validation: certified\n", "", `"validation" header is mandatory`},
{"validation: certified\n", "validation: \n", `"validation" header should not be empty`},
{s.tsLine, "", `"timestamp" header is mandatory`},
Expand Down

0 comments on commit 0bf01a5

Please sign in to comment.