all repos

onasty @ 336ceb20f502c323ae2ae7afb873ec5b0da136e1

a one-time notes service

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

Smirnov Oleksandr Smirnov Oleksandr
ss2316544@gmail.com
feat: notes manipulations for the note authors (#117)..., 1 year ago
1
package models
2
3
import (
4
	"testing"
5
	"time"
6
7
	assert "github.com/stretchr/testify/require"
8
)
9
10
func TestNote_Validate(t *testing.T) {
11
	// NOTE: there no need to test if note is expired since it tested in IsExpired test
12
13
	t.Run("should pass the validation only if content provided", func(t *testing.T) {
14
		n := Note{Content: "the content"} //nolint:exhaustruct
15
		assert.NoError(t, n.Validate())
16
	})
17
	t.Run("should pass validation with content and correct expiration time", func(t *testing.T) {
18
		n := Note{ //nolint:exhaustruct
19
			Content:   "content",
20
			ExpiresAt: time.Now().Add(time.Minute),
21
		}
22
		assert.NoError(t, n.Validate())
23
	})
24
	t.Run("should fail if content is missing", func(t *testing.T) {
25
		n := Note{Content: ""} //nolint:exhaustruct
26
		assert.EqualError(t, n.Validate(), ErrNoteContentIsEmpty.Error())
27
	})
28
	t.Run("should fail if content is missing and other fields are set", func(t *testing.T) {
29
		n := Note{ //nolint:exhaustruct
30
			Slug:                 "some-slug",
31
			Password:             "some-password",
32
			BurnBeforeExpiration: false,
33
		}
34
		assert.EqualError(t, n.Validate(), ErrNoteContentIsEmpty.Error())
35
	})
36
	t.Run("should fail if expiration time is in the past", func(t *testing.T) {
37
		n := Note{Content: "content", ExpiresAt: time.Now().Add(-time.Hour)} //nolint:exhaustruct
38
		assert.EqualError(t, n.Validate(), ErrNoteExpired.Error())
39
	})
40
}
41
42
func TestNote_IsExpired(t *testing.T) {
43
	t.Run("should be expired", func(t *testing.T) {
44
		note := Note{ExpiresAt: time.Now().Add(-time.Hour)} //nolint:exhaustruct
45
		assert.True(t, note.IsExpired())
46
	})
47
	t.Run("should be not expired", func(t *testing.T) {
48
		note := Note{ExpiresAt: time.Now().Add(time.Hour)} //nolint:exhaustruct
49
		assert.False(t, note.IsExpired())
50
	})
51
	t.Run("should be not expired when [ExpiredAt] is zero", func(t *testing.T) {
52
		note := Note{ExpiresAt: time.Time{}} //nolint:exhaustruct
53
		assert.False(t, note.IsExpired())
54
	})
55
}
56
57
func TestNote_ShouldBeBurnt(t *testing.T) {
58
	t.Run("should be burnt", func(t *testing.T) {
59
		note := Note{ //nolint:exhaustruct
60
			BurnBeforeExpiration: true,
61
			ExpiresAt:            time.Now().Add(time.Hour),
62
		}
63
		assert.True(t, note.ShouldBeBurnt())
64
	})
65
	t.Run("should not be burnt", func(t *testing.T) {
66
		note := Note{ //nolint:exhaustruct
67
			BurnBeforeExpiration: true,
68
			ExpiresAt:            time.Time{},
69
		}
70
		assert.False(t, note.ShouldBeBurnt())
71
	})
72
	t.Run("could not be burnt when expiration and shouldBurn set to false", func(t *testing.T) {
73
		note := Note{ //nolint:exhaustruct
74
			BurnBeforeExpiration: false,
75
			ExpiresAt:            time.Time{},
76
		}
77
		assert.False(t, note.ShouldBeBurnt())
78
	})
79
}
80
81
func TestNote_IsRead(t *testing.T) {
82
	t.Run("should be unread", func(t *testing.T) {
83
		n := Note{ReadAt: time.Time{}} //nolint:exhaustruct
84
		assert.False(t, n.IsRead())
85
	})
86
	t.Run("should be read", func(t *testing.T) {
87
		n := Note{ReadAt: time.Now()} //nolint:exhaustruct
88
		assert.True(t, n.IsRead())
89
	})
90
}