onasty/internal/models/note_test.go (view raw)
Olexandr Smirnov
Olexandr Smirnov
ss2316544@gmail.com fix: return "invalid slug" error to user (#189)..., 9 months ago
ss2316544@gmail.com fix: return "invalid slug" error to user (#189)..., 9 months ago
| 1 | package models |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | "time" |
| 6 | |
| 7 | assert "github.com/stretchr/testify/require" |
| 8 | ) |
| 9 | |
| 10 | //nolint:exhaustruct |
| 11 | func TestNote_Validate(t *testing.T) { |
| 12 | // NOTE: there no need to test if note is expired since it tested in IsExpired test |
| 13 | |
| 14 | t.Run("should pass the validation if only slug and content are provided", func(t *testing.T) { |
| 15 | n := Note{Content: "the content", Slug: "s"} |
| 16 | assert.NoError(t, n.Validate()) |
| 17 | }) |
| 18 | t.Run("should pass validation with content and correct expiration time", func(t *testing.T) { |
| 19 | n := Note{ |
| 20 | Content: "content", |
| 21 | Slug: "s", |
| 22 | ExpiresAt: time.Now().Add(time.Minute), |
| 23 | } |
| 24 | assert.NoError(t, n.Validate()) |
| 25 | }) |
| 26 | t.Run("should fail if content is missing", func(t *testing.T) { |
| 27 | n := Note{Content: ""} |
| 28 | assert.EqualError(t, n.Validate(), ErrNoteContentIsEmpty.Error()) |
| 29 | }) |
| 30 | t.Run("should fail if content is missing and other fields are set", func(t *testing.T) { |
| 31 | n := Note{ |
| 32 | Slug: "some-slug", |
| 33 | Password: "some-password", |
| 34 | BurnBeforeExpiration: false, |
| 35 | } |
| 36 | assert.EqualError(t, n.Validate(), ErrNoteContentIsEmpty.Error()) |
| 37 | }) |
| 38 | t.Run("should fail if expiration time is in the past", func(t *testing.T) { |
| 39 | n := Note{ |
| 40 | Content: "content", |
| 41 | Slug: "s", |
| 42 | ExpiresAt: time.Now().Add(-time.Hour), |
| 43 | } |
| 44 | assert.EqualError(t, n.Validate(), ErrNoteExpired.Error()) |
| 45 | }) |
| 46 | t.Run("should fail if slug is empty", func(t *testing.T) { |
| 47 | n := Note{Content: "the content", Slug: " "} |
| 48 | assert.EqualError(t, n.Validate(), ErrNoteSlugIsInvalid.Error()) |
| 49 | }) |
| 50 | t.Run("should fail if slug has '/'", func(t *testing.T) { |
| 51 | n := Note{Content: "the content", Slug: "asdf/asdf"} |
| 52 | assert.EqualError(t, n.Validate(), ErrNoteSlugIsInvalid.Error()) |
| 53 | }) |
| 54 | t.Run("should fail if slug one of not allowed slugs", func(t *testing.T) { |
| 55 | for notAllowedSlug := range notAllowedSlugs { |
| 56 | n := Note{Content: "the content", Slug: notAllowedSlug} |
| 57 | assert.EqualError(t, n.Validate(), ErrNoteSlugIsAlreadyInUse.Error()) |
| 58 | } |
| 59 | }) |
| 60 | } |
| 61 | |
| 62 | //nolint:exhaustruct |
| 63 | func TestNote_IsExpired(t *testing.T) { |
| 64 | t.Run("should be expired", func(t *testing.T) { |
| 65 | note := Note{ExpiresAt: time.Now().Add(-time.Hour)} |
| 66 | assert.True(t, note.IsExpired()) |
| 67 | }) |
| 68 | t.Run("should be not expired", func(t *testing.T) { |
| 69 | note := Note{ExpiresAt: time.Now().Add(time.Hour)} |
| 70 | assert.False(t, note.IsExpired()) |
| 71 | }) |
| 72 | t.Run("should be not expired when [ExpiredAt] is zero", func(t *testing.T) { |
| 73 | note := Note{ExpiresAt: time.Time{}} |
| 74 | assert.False(t, note.IsExpired()) |
| 75 | }) |
| 76 | } |
| 77 | |
| 78 | //nolint:exhaustruct |
| 79 | func TestNote_ShouldBeBurnt(t *testing.T) { |
| 80 | t.Run("should be burnt", func(t *testing.T) { |
| 81 | note := Note{ |
| 82 | BurnBeforeExpiration: true, |
| 83 | ExpiresAt: time.Now().Add(time.Hour), |
| 84 | } |
| 85 | assert.True(t, note.ShouldBeBurnt()) |
| 86 | }) |
| 87 | t.Run("should not be burnt", func(t *testing.T) { |
| 88 | note := Note{ |
| 89 | BurnBeforeExpiration: true, |
| 90 | ExpiresAt: time.Time{}, |
| 91 | } |
| 92 | assert.False(t, note.ShouldBeBurnt()) |
| 93 | }) |
| 94 | t.Run("could not be burnt when expiration and shouldBurn set to false", func(t *testing.T) { |
| 95 | note := Note{ |
| 96 | BurnBeforeExpiration: false, |
| 97 | ExpiresAt: time.Time{}, |
| 98 | } |
| 99 | assert.False(t, note.ShouldBeBurnt()) |
| 100 | }) |
| 101 | } |
| 102 | |
| 103 | //nolint:exhaustruct |
| 104 | func TestNote_IsRead(t *testing.T) { |
| 105 | t.Run("should be unread", func(t *testing.T) { |
| 106 | n := Note{ReadAt: time.Time{}} |
| 107 | assert.False(t, n.IsRead()) |
| 108 | }) |
| 109 | t.Run("should be read", func(t *testing.T) { |
| 110 | n := Note{ReadAt: time.Now()} |
| 111 | assert.True(t, n.IsRead()) |
| 112 | }) |
| 113 | } |