package store import ( "testing" "time" "github.com/pkg/errors" "github.com/stretchr/testify/assert" ) func TestComment_Sanitize(t *testing.T) { tbl := []struct { inp Comment out Comment }{ {inp: Comment{}, out: Comment{}}, { inp: Comment{ Text: `blah XSS` + "\n\t", User: User{ID: `username`}, }, out: Comment{ Text: "blah XSS\n\t", User: User{ID: `<a href="http://blah.com">username</a>`}, }, }, } for n, tt := range tbl { tt.inp.Sanitize() assert.Equal(t, tt.out, tt.inp, "check #%d", n) } } func TestComment_Validate(t *testing.T) { longText := "" for i := 0; i < 4000; i++ { longText += "X" } tbl := []struct { inp Comment err error }{ {inp: Comment{}, err: errors.New("empty comment text")}, {inp: Comment{Text: "something blah", User: User{ID: "myid", Name: "name"}}, err: nil}, {inp: Comment{Text: "something blah", User: User{ID: "myid"}}, err: errors.New("empty user info")}, {inp: Comment{Text: longText, User: User{ID: "myid", Name: "name"}}, err: errors.New("comment text exceeded max allowed size")}, } for n, tt := range tbl { e := tt.inp.Validate() if tt.err == nil { assert.Nil(t, e, "check #%d", n) continue } assert.EqualError(t, tt.err, e.Error(), "check #%d", n) } } func TestComment_PrepareUntrusted(t *testing.T) { comment := Comment{ Text: `blah`, User: User{ID: "username"}, ParentID: "p123", ID: "123", Locator: Locator{SiteID: "site", URL: "url"}, Score: 10, Pin: true, Deleted: true, Timestamp: time.Date(2018, 1, 1, 9, 30, 0, 0, time.Local), Votes: map[string]bool{"uu": true}, } comment.PrepareUntrusted() assert.Equal(t, "", comment.ID) assert.Equal(t, "p123", comment.ParentID) assert.Equal(t, "blah", comment.Text) assert.Equal(t, 0, comment.Score) assert.Equal(t, false, comment.Pin) assert.Equal(t, time.Time{}, comment.Timestamp) assert.Equal(t, false, comment.Deleted) assert.Equal(t, make(map[string]bool), comment.Votes) assert.Equal(t, User{ID: "username"}, comment.User) }