onasty/internal/models/note_test.go (view raw)
Oleksandr Smirnov
Oleksandr Smirnov
olexsmir@gmail.com refactor!: rename "burn before expiration" to "keep before expiration" (#199)..., 9 months ago
olexsmir@gmail.com refactor!: rename "burn before expiration" to "keep before expiration" (#199)..., 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 | KeepBeforeExpiration: 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 keep before expiration is set, and expiration time is not", |
| 47 | func(t *testing.T) { |
| 48 | n := Note{ |
| 49 | Content: "content", |
| 50 | KeepBeforeExpiration: true, |
| 51 | } |
| 52 | |
| 53 | assert.EqualError(t, n.Validate(), ErrNoteCannotBeKept.Error()) |
| 54 | }, |
| 55 | ) |
| 56 | t.Run("should not fail if slug is not provided", func(t *testing.T) { |
| 57 | n := Note{Content: "the content"} |
| 58 | assert.NoError(t, n.Validate()) |
| 59 | }) |
| 60 | t.Run("should fail if slug is empty", func(t *testing.T) { |
| 61 | n := Note{Content: "the content", Slug: " "} |
| 62 | assert.EqualError(t, n.Validate(), ErrNoteSlugIsInvalid.Error()) |
| 63 | }) |
| 64 | t.Run("should fail if slug has '/'", func(t *testing.T) { |
| 65 | n := Note{Content: "the content", Slug: "asdf/asdf"} |
| 66 | assert.EqualError(t, n.Validate(), ErrNoteSlugIsInvalid.Error()) |
| 67 | }) |
| 68 | t.Run("should fail if slug one of not allowed slugs", func(t *testing.T) { |
| 69 | for notAllowedSlug := range notAllowedSlugs { |
| 70 | n := Note{Content: "the content", Slug: notAllowedSlug} |
| 71 | assert.EqualError(t, n.Validate(), ErrNoteSlugIsAlreadyInUse.Error()) |
| 72 | } |
| 73 | }) |
| 74 | } |
| 75 | |
| 76 | //nolint:exhaustruct |
| 77 | func TestNote_IsExpired(t *testing.T) { |
| 78 | t.Run("should be expired", func(t *testing.T) { |
| 79 | note := Note{ExpiresAt: time.Now().Add(-time.Hour)} |
| 80 | assert.True(t, note.IsExpired()) |
| 81 | }) |
| 82 | t.Run("should be not expired", func(t *testing.T) { |
| 83 | note := Note{ExpiresAt: time.Now().Add(time.Hour)} |
| 84 | assert.False(t, note.IsExpired()) |
| 85 | }) |
| 86 | t.Run("should be not expired when [ExpiredAt] is zero", func(t *testing.T) { |
| 87 | note := Note{ExpiresAt: time.Time{}} |
| 88 | assert.False(t, note.IsExpired()) |
| 89 | }) |
| 90 | } |
| 91 | |
| 92 | //nolint:exhaustruct |
| 93 | func TestNote_ShouldPreserveOnRead(t *testing.T) { |
| 94 | t.Run("should keep", func(t *testing.T) { |
| 95 | note := Note{ |
| 96 | KeepBeforeExpiration: true, |
| 97 | ExpiresAt: time.Now().Add(time.Hour), |
| 98 | } |
| 99 | assert.True(t, note.ShouldPreserveOnRead()) |
| 100 | }) |
| 101 | t.Run("should not be kept", func(t *testing.T) { |
| 102 | note := Note{ |
| 103 | KeepBeforeExpiration: true, |
| 104 | ExpiresAt: time.Time{}, |
| 105 | } |
| 106 | assert.False(t, note.ShouldPreserveOnRead()) |
| 107 | }) |
| 108 | t.Run("cannot be kept when expiration and shouldKeep set to false", func(t *testing.T) { |
| 109 | note := Note{ |
| 110 | KeepBeforeExpiration: false, |
| 111 | ExpiresAt: time.Time{}, |
| 112 | } |
| 113 | assert.False(t, note.ShouldPreserveOnRead()) |
| 114 | }) |
| 115 | } |
| 116 | |
| 117 | //nolint:exhaustruct |
| 118 | func TestNote_IsRead(t *testing.T) { |
| 119 | t.Run("should be unread", func(t *testing.T) { |
| 120 | n := Note{ReadAt: time.Time{}} |
| 121 | assert.False(t, n.IsRead()) |
| 122 | }) |
| 123 | t.Run("should be read", func(t *testing.T) { |
| 124 | n := Note{ReadAt: time.Now()} |
| 125 | assert.True(t, n.IsRead()) |
| 126 | }) |
| 127 | } |