all repos

onasty @ main

a one-time notes service

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package models

import (
	"testing"
	"time"

	assert "github.com/stretchr/testify/require"
)

//nolint:exhaustruct
func TestNote_Validate(t *testing.T) {
	// NOTE: there no need to test if note is expired since it tested in IsExpired test

	t.Run("should pass the validation if only slug and content are provided", func(t *testing.T) {
		n := Note{Content: "the content", Slug: "s"}
		assert.NoError(t, n.Validate())
	})
	t.Run("should pass validation with content and correct expiration time", func(t *testing.T) {
		n := Note{
			Content:   "content",
			Slug:      "s",
			ExpiresAt: time.Now().Add(time.Minute),
		}
		assert.NoError(t, n.Validate())
	})
	t.Run("should fail if content is missing", func(t *testing.T) {
		n := Note{Content: ""}
		assert.EqualError(t, n.Validate(), ErrNoteContentIsEmpty.Error())
	})
	t.Run("should fail if content is missing and other fields are set", func(t *testing.T) {
		n := Note{
			Slug:                 "some-slug",
			Password:             "some-password",
			KeepBeforeExpiration: false,
		}
		assert.EqualError(t, n.Validate(), ErrNoteContentIsEmpty.Error())
	})
	t.Run("should fail if expiration time is in the past", func(t *testing.T) {
		n := Note{
			Content:   "content",
			Slug:      "s",
			ExpiresAt: time.Now().Add(-time.Hour),
		}
		assert.EqualError(t, n.Validate(), ErrNoteExpired.Error())
	})
	t.Run("should fail if keep before expiration is set, and expiration time is not",
		func(t *testing.T) {
			n := Note{
				Content:              "content",
				KeepBeforeExpiration: true,
			}

			assert.EqualError(t, n.Validate(), ErrNoteCannotBeKept.Error())
		},
	)
	t.Run("should not fail if slug is not provided", func(t *testing.T) {
		n := Note{Content: "the content"}
		assert.NoError(t, n.Validate())
	})
	t.Run("should fail if slug is empty", func(t *testing.T) {
		n := Note{Content: "the content", Slug: " "}
		assert.EqualError(t, n.Validate(), ErrNoteSlugIsInvalid.Error())
	})
	t.Run("should fail if slug has '/'", func(t *testing.T) {
		n := Note{Content: "the content", Slug: "asdf/asdf"}
		assert.EqualError(t, n.Validate(), ErrNoteSlugIsInvalid.Error())
	})
	t.Run("should fail if slug one of not allowed slugs", func(t *testing.T) {
		for notAllowedSlug := range notAllowedSlugs {
			n := Note{Content: "the content", Slug: notAllowedSlug}
			assert.EqualError(t, n.Validate(), ErrNoteSlugIsAlreadyInUse.Error())
		}
	})
}

//nolint:exhaustruct
func TestNote_IsExpired(t *testing.T) {
	t.Run("should be expired", func(t *testing.T) {
		note := Note{ExpiresAt: time.Now().Add(-time.Hour)}
		assert.True(t, note.IsExpired())
	})
	t.Run("should be not expired", func(t *testing.T) {
		note := Note{ExpiresAt: time.Now().Add(time.Hour)}
		assert.False(t, note.IsExpired())
	})
	t.Run("should be not expired when [ExpiredAt] is zero", func(t *testing.T) {
		note := Note{ExpiresAt: time.Time{}}
		assert.False(t, note.IsExpired())
	})
}

//nolint:exhaustruct
func TestNote_ShouldPreserveOnRead(t *testing.T) {
	t.Run("should keep", func(t *testing.T) {
		note := Note{
			KeepBeforeExpiration: true,
			ExpiresAt:            time.Now().Add(time.Hour),
		}
		assert.True(t, note.ShouldPreserveOnRead())
	})
	t.Run("should not be kept", func(t *testing.T) {
		note := Note{
			KeepBeforeExpiration: true,
			ExpiresAt:            time.Time{},
		}
		assert.False(t, note.ShouldPreserveOnRead())
	})
	t.Run("cannot be kept when expiration and shouldKeep set to false", func(t *testing.T) {
		note := Note{
			KeepBeforeExpiration: false,
			ExpiresAt:            time.Time{},
		}
		assert.False(t, note.ShouldPreserveOnRead())
	})
}

//nolint:exhaustruct
func TestNote_IsRead(t *testing.T) {
	t.Run("should be unread", func(t *testing.T) {
		n := Note{ReadAt: time.Time{}}
		assert.False(t, n.IsRead())
	})
	t.Run("should be read", func(t *testing.T) {
		n := Note{ReadAt: time.Now()}
		assert.True(t, n.IsRead())
	})
}