-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsubscriber_test.go
More file actions
130 lines (100 loc) · 4.13 KB
/
subscriber_test.go
File metadata and controls
130 lines (100 loc) · 4.13 KB
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
128
129
130
package subscribe
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestCreateSub(t *testing.T) {
t.Parallel()
assertions := assert.New(t)
sub := &Subscribe{Events: new(Events)}
sub.CreateSub("myContacNameTest", "apiValueHere", true, false)
assertions.Len(sub.Subscribers, 1, "there must be one subscriber")
assertions.True(sub.Subscribers[0].Admin, "admin must be true")
assertions.False(sub.Subscribers[0].Ignored, "ignore must be false")
// Update values for existing contact.
sub.CreateSub("myContacNameTest", "apiValueHere", false, true)
assertions.Len(sub.Subscribers, 1, "there must still be one subscriber")
assertions.False(sub.Subscribers[0].Admin, "admin must be changed to false")
assertions.True(sub.Subscribers[0].Ignored, "ignore must be changed to true")
assertions.Equal("myContacNameTest", sub.Subscribers[0].Contact, "contact value is incorrect")
assertions.Equal("apiValueHere", sub.Subscribers[0].API, "api value is incorrect")
// Add another contact.
sub.CreateSub("myContacName2Test", "apiValueHere", false, true)
assertions.Len(sub.Subscribers, 2, "there must be two subscribers")
assertions.NotNil(sub.Subscribers[1].Events, "events map must not be nil")
}
func TestGetSubscriber(t *testing.T) {
t.Parallel()
assertions := assert.New(t)
sub := &Subscribe{Events: new(Events)}
// Test missing subscriber
_, err := sub.GetSubscriber("im not here", "fake")
assertions.Equal(ErrSubscriberNotFound, err, "must have a subscriber not found error")
// Test getting real subscriber
sub.CreateSub("myContacNameTest", "apiValueHere", true, false)
_, err = sub.GetSubscriber("myContacNameTest", "apiValueHere")
assertions.NoError(err, "must not produce an error getting existing subscriber")
}
func TestAdmin(t *testing.T) {
t.Parallel()
assertions := assert.New(t)
sub := &Subscribe{Events: new(Events)}
// Test missing subscriber
subs := sub.GetAdmins()
assertions.Empty(subs, "there must be zero admin since none were added")
// Test getting real subscriber
sub.CreateSub("myContacNameTest", "apiValueHere", true, false)
sub.CreateSub("myContacNameTest2", "apiValueHere", false, false)
sub.CreateSub("myContacNameTest3", "apiValueHere", false, false)
subs = sub.GetAdmins()
assertions.Len(subs, 1, "there must be one admin")
assertions.Equal("myContacNameTest", subs[0].Contact)
}
func TestIgnore(t *testing.T) {
t.Parallel()
assertions := assert.New(t)
sub := &Subscribe{Events: new(Events)}
// Test missing subscriber
subs := sub.GetIgnored()
assertions.Empty(subs, "there must be zero ignored users since none were added")
// Test getting real subscriber
sub.CreateSub("myContacNameTest", "apiValueHere", false, true)
sub.CreateSub("myContacNameTest1", "apiValueHere", true, false)
sub.CreateSub("myContacNameTest2", "apiValueHere", false, false)
sub.CreateSub("myContacNameTest3", "apiValueHere", false, false)
subs = sub.GetIgnored()
assertions.Len(subs, 1, "there must be one ignored user")
assertions.Equal("myContacNameTest", subs[0].Contact)
}
func TestCreateSubWithID(t *testing.T) {
t.Parallel()
sub := &Subscribe{Events: new(Events)}
assert.Nil(t, sub.CreateSubWithID(0, "contact", "api", true, false))
first := sub.CreateSubWithID(10, "contact", "api", true, false)
require.NotNil(t, first)
assert.EqualValues(t, 10, first.ID)
assert.True(t, first.Admin)
assert.False(t, first.Ignored)
assert.Len(t, sub.Subscribers, 1)
second := sub.CreateSubWithID(10, "contact-new", "api", false, true)
require.NotNil(t, second)
assert.Same(t, first, second)
assert.False(t, second.Admin)
assert.True(t, second.Ignored)
assert.Equal(t, "contact", second.Contact)
assert.Len(t, sub.Subscribers, 1)
}
func TestGetSubscriberByID(t *testing.T) {
t.Parallel()
sub := &Subscribe{Events: new(Events)}
_, err := sub.GetSubscriberByID(0, "api")
assert.Equal(t, ErrSubscriberNotFound, err)
sub.CreateSubWithID(99, "contact", "api", true, false)
got, err := sub.GetSubscriberByID(99, "api")
require.NoError(t, err)
require.NotNil(t, got)
assert.EqualValues(t, 99, got.ID)
_, err = sub.GetSubscriberByID(99, "api2")
assert.Equal(t, ErrSubscriberNotFound, err)
}