all repos

onasty @ a705d9c2248d300dee212abc6a1e453379f84d3a

a one-time notes service

onasty/internal/models/note_test.go (view raw)

Olexandr Smirnov Olexandr Smirnov
ss2316544@gmail.com
feat(api): validate user provided slug (#164)..., 11 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 is empty", func(t *testing.T) {
51
		n := Note{Content: "the content", Slug: " "}
52
		assert.EqualError(t, n.Validate(), ErrNoteSlugIsInvalid.Error())
53
	})
54
}
55
56
//nolint:exhaustruct
57
func TestNote_IsExpired(t *testing.T) {
58
	t.Run("should be expired", func(t *testing.T) {
59
		note := Note{ExpiresAt: time.Now().Add(-time.Hour)}
60
		assert.True(t, note.IsExpired())
61
	})
62
	t.Run("should be not expired", func(t *testing.T) {
63
		note := Note{ExpiresAt: time.Now().Add(time.Hour)}
64
		assert.False(t, note.IsExpired())
65
	})
66
	t.Run("should be not expired when [ExpiredAt] is zero", func(t *testing.T) {
67
		note := Note{ExpiresAt: time.Time{}}
68
		assert.False(t, note.IsExpired())
69
	})
70
}
71
72
//nolint:exhaustruct
73
func TestNote_ShouldBeBurnt(t *testing.T) {
74
	t.Run("should be burnt", func(t *testing.T) {
75
		note := Note{
76
			BurnBeforeExpiration: true,
77
			ExpiresAt:            time.Now().Add(time.Hour),
78
		}
79
		assert.True(t, note.ShouldBeBurnt())
80
	})
81
	t.Run("should not be burnt", func(t *testing.T) {
82
		note := Note{
83
			BurnBeforeExpiration: true,
84
			ExpiresAt:            time.Time{},
85
		}
86
		assert.False(t, note.ShouldBeBurnt())
87
	})
88
	t.Run("could not be burnt when expiration and shouldBurn set to false", func(t *testing.T) {
89
		note := Note{
90
			BurnBeforeExpiration: false,
91
			ExpiresAt:            time.Time{},
92
		}
93
		assert.False(t, note.ShouldBeBurnt())
94
	})
95
}
96
97
//nolint:exhaustruct
98
func TestNote_IsRead(t *testing.T) {
99
	t.Run("should be unread", func(t *testing.T) {
100
		n := Note{ReadAt: time.Time{}}
101
		assert.False(t, n.IsRead())
102
	})
103
	t.Run("should be read", func(t *testing.T) {
104
		n := Note{ReadAt: time.Now()}
105
		assert.True(t, n.IsRead())
106
	})
107
}