-
Notifications
You must be signed in to change notification settings - Fork 2
/
notifications_test.go
79 lines (71 loc) · 1.86 KB
/
notifications_test.go
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
package main
import (
"context"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"testing"
)
func TestNotifsInit(t *testing.T) {
expected := "foo"
NotifsInit(expected)
actual := NotifsURI
if actual != expected {
t.Errorf("uri was %s, not %s", actual, expected)
}
}
func TestNewNotification(t *testing.T) {
expectedURI := "foo"
expectedUser := "user"
expectedSubject := "subject"
expectedTemplate := "analysis_status_change"
NotifsInit(expectedURI)
n := NewNotification(expectedUser, expectedSubject, "", true, expectedTemplate, nil)
if n.URI != expectedURI {
t.Errorf("URI was %s, not %s", n.URI, expectedURI)
}
if n.User != expectedUser {
t.Errorf("User was %s, not %s", n.User, expectedUser)
}
if n.Subject != expectedSubject {
t.Errorf("Subject was %s, not %s", n.Subject, expectedSubject)
}
if n.EmailTemplate != expectedTemplate {
t.Errorf("EmailTemplate was %s, not %s", n.EmailTemplate, expectedTemplate)
}
}
func TestSend(t *testing.T) {
expectedUser := "test-user"
expectedSubject := "test-subject"
n := NewNotification(expectedUser, expectedSubject, "", true, "analysis_status_change", nil)
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, err := io.ReadAll(r.Body)
if err != nil {
t.Error(err)
}
n1 := &Notification{}
err = json.Unmarshal(b, n1)
if err != nil {
t.Error(err)
}
if n1.User != expectedUser {
t.Errorf("user was %s, not %s", n1.User, expectedUser)
}
if n1.Subject != expectedSubject {
t.Errorf("subject was %s, not %s", n1.Subject, expectedSubject)
}
if n1.Type != "analysis" {
t.Errorf("type was %s, not 'Analysis'", n1.Type)
}
}))
defer srv.Close()
n.URI = srv.URL
resp, err := n.Send(context.Background())
if err != nil {
t.Error(err)
}
if resp.StatusCode != 200 {
t.Errorf("status code was %d, not 200", resp.StatusCode)
}
}