This repository has been archived by the owner on Jan 25, 2024. It is now read-only.
/
webinvoice_test.go
107 lines (97 loc) · 2.32 KB
/
webinvoice_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
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
package nlgids
import (
"io/ioutil"
"path"
"strings"
"testing"
"time"
"github.com/miekg/nlgids/date"
"github.com/miekg/nlgids/email"
"github.com/miekg/nlgids/tour"
"github.com/miekg/nlgids/webinvoice"
)
const TestFileName = "reservering-10-december-2015.pdf"
func newInvoice() *webinvoice.Invoice {
i := &webinvoice.Invoice{
Tour: "walks/koninklijke",
Persons: 2,
Time: "11:00",
Duration: "2.0",
Cost: 50.0,
Date: "2015-12-10",
Name: "Christel",
FullName: "Christel Achternaam",
Email: "christel_bla@miek.nl",
Where: "Green Park metro station",
How: "Ik sta buiten de de fontein om",
Kenmerk: webinvoice.Kenmerk(time.Now().UTC()),
}
i.Tour = tour.NameOrNonExists(i.Tour, "/home/miek/html/nlgids.london/tours.json")
return i
}
func TestInvoiceFill(t *testing.T) {
i := newInvoice()
err := i.FillOut()
if err != nil {
t.Fatal(err)
}
if i.Day == "" {
t.Fatal("should be non empty: 'Day'")
}
if i.FileName == "" {
t.Fatal("should be non empty: 'FileName'")
}
if i.FileName != TestFileName {
t.Fatalf("filename should be %s, got %s", TestFileName, i.FileName)
}
if i.Rate == 0 {
t.Fatal("should be non empty: 'Rate'")
}
}
func TestTexFiles(t *testing.T) {
sources, err := webinvoice.TeXFiles(templateDir)
if err != nil {
t.Fatal(err)
}
if len(sources) == 0 {
t.Fatalf("no sources found in %s", templateDir)
}
}
func TestInvoiceCreate(t *testing.T) {
invoice := path.Join(templateDir, webinvoice.Template)
i := newInvoice()
pdf, err := i.Create(templateDir, invoice)
if err != nil {
t.Fatal(err)
}
if len(pdf) == 0 {
t.Fatal("no pdf produced")
}
ioutil.WriteFile("/tmp/test.pdf", pdf, 0644)
body, err := i.MailBody()
if err != nil {
t.Fatal(err)
}
mail := email.NewInvoice(i.MailSubject(), body, i.FileName, pdf)
if strings.Contains("Christel Achternaam", mail.Subject) {
t.Fatal("wrong email Subject")
}
if mail.From != "" { // Set in mail.Do()
t.Fatal("wrong email From")
}
if len(mail.Cc) != 0 {
t.Fatal("wrong email Cc")
}
if len(mail.Attachments) != 1 {
t.Fatal("wrong email attachment number")
}
}
func TestInvoiceCalendarLink(t *testing.T) {
i := newInvoice()
t1, t2, err := date.TimeToUTC(i.Date, i.Time)
if err != nil {
t.Fatalf("Failed to create times: %s", err)
}
link := i.CalendarLink(t1, t2)
t.Logf("%s", link)
}