-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
73 lines (62 loc) · 1.47 KB
/
main_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
package main
import (
"context"
"io"
"os"
"testing"
"time"
"github.com/stretchr/testify/require"
)
type Email struct {
to string
content string
}
type MockSender struct {
emails []Email
}
func (m *MockSender) Send(ctx context.Context, to, content string) error {
e := Email{
to: to,
content: content,
}
m.emails = append(m.emails, e)
return nil
}
func Test_nofity(t *testing.T) {
observers := map[string][]string{
"Book 1": {"[email protected]", "[email protected]"},
"Book 2": {"[email protected]"},
}
builds := []Build{
{1, "Book 1", true},
{2, "Book 1", false},
{3, "Book 2", true},
}
var s MockSender
db, err := OpenDB("/dev/null")
require.NoError(t, err, "db")
notify(context.Background(), builds, observers, db, &s)
require.Equal(t, 3, len(s.emails), "email count")
}
func Test_fetchBuilds(t *testing.T) {
user, passwd := os.Getenv("PRAG_USER"), os.Getenv("PRAG_PASSWD")
if user == "" || passwd == "" {
t.Skip("missing credentials")
}
var ctx context.Context
var cancel context.CancelFunc
d, ok := t.Deadline()
if ok {
ctx, cancel = context.WithDeadline(context.Background(), d)
} else {
ctx, cancel = context.WithTimeout(context.Background(), 3*time.Second)
}
defer cancel()
r, err := fetchBuilds(ctx, user, passwd)
require.NoError(t, err, "fetch")
defer r.Close()
data, err := io.ReadAll(io.LimitReader(r, 1_000_000))
require.NoError(t, err, "read")
html := string(data)
require.Contains(t, html, `<table id="bookshelf"`, "html")
}