This repository has been archived by the owner on Mar 30, 2020. It is now read-only.
forked from adlio/harvest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrealworld_test.go
304 lines (246 loc) · 6.68 KB
/
realworld_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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package harvest_api_client
import (
"os"
"testing"
"time"
)
func TestRealWorldGetTasks(t *testing.T) {
api := realWorldTestAPI(t)
tasks, err := api.GetTasks(Defaults())
if err != nil {
t.Error(err)
}
if len(tasks) < 1 {
t.Error("GetTasks() returned no tasks. Are you testing with an empty Harvest account?")
}
task, err := api.GetTask(tasks[0].ID, Defaults())
if err != nil {
t.Error(err)
}
if task.Name == "" {
t.Error("Task name was blank")
}
}
func TestRealWorldGetClients(t *testing.T) {
api := realWorldTestAPI(t)
clients, err := api.GetClients(Defaults())
if err != nil {
t.Error(err)
}
if len(clients) < 1 {
t.Error("GetClients() returned no clients. Are you testing with an empty Harvest account?")
}
client, err := api.GetClient(clients[0].ID, Defaults())
if err != nil {
t.Error(err)
}
if client.Name == "" {
t.Error("Client name was blank")
}
_, err = api.GetClientContacts(client.ID, Defaults())
if err != nil {
t.Error(err)
}
}
func TestRealWorldGetProjects(t *testing.T) {
api := realWorldTestAPI(t)
projects, err := api.GetProjects(Defaults())
if err != nil {
t.Error(err)
}
if len(projects) < 1 {
t.Error("GetProjects() returned no projects. Are you testing with an empty Harvest account?")
}
project, err := api.GetProject(projects[0].ID, Defaults())
if err != nil {
t.Error(err)
}
if project.Name == "" {
t.Error("Project name was blank")
}
uas, err := api.GetUserAssignments(project.ID, Defaults())
if err != nil {
t.Error(err)
}
if len(uas) < 1 {
t.Errorf("Project %d %s didn't contain any user assignments.", project.ID, project.Name)
}
tas, err := api.GetTaskAssignments(project.ID, Defaults())
if err != nil {
t.Error(err)
}
if len(tas) < 1 {
t.Errorf("Project #%d %s didn't contain any task assignments.", project.ID, project.Name)
}
_, err = api.GetTimeEntriesForProjectBetween(project.ID, time.Now().AddDate(-10, 0, 0), time.Now(), Defaults())
if err != nil {
t.Error(err)
}
}
func TestRealWorldGetTimeEntries(t *testing.T) {
api := realWorldTestAPI(t)
tes, err := api.GetTimeEntriesBetween(time.Now().AddDate(0, 0, -7), time.Now(), Defaults())
if err != nil {
t.Error(err)
}
if len(tes) < 1 {
t.Error("GetTimeEntriesBetween() failed to find time entries in the last 7 days. Is this an empty Harvest account?")
}
}
func TestRealWorldGetExpenses(t *testing.T) {
api := realWorldTestAPI(t)
expenses, err := api.GetExpenses(Defaults())
if err != nil {
t.Fatal(err)
}
if len(expenses) < 1 {
t.Error("GetExpenses() returned no expenses. Are you testing with an empty Harvest account?")
}
expense, err := api.GetExpense(expenses[0].ID, Defaults())
if err != nil {
t.Error(err)
}
if expense.TotalCost == 0 {
t.Error("Got an expense with no TotalCost.")
}
}
func TestRealWorldGetExpenseCategories(t *testing.T) {
api := realWorldTestAPI(t)
categories, err := api.GetExpenseCategories(Defaults())
if err != nil {
t.Error(err)
}
if len(categories) < 1 {
t.Error("GetExpenseCategories() returned no categories. Are you testing with an empty Harvest account?")
}
category, err := api.GetExpenseCategory(categories[0].ID, Defaults())
if err != nil {
t.Error(err)
}
if category.Name == "" {
t.Error("Category didn't have a name")
}
}
func TestRealWorldGetInvoices(t *testing.T) {
api := realWorldTestAPI(t)
invoices, err := api.GetInvoices(Defaults())
if err != nil {
t.Error(err)
}
if len(invoices) < 1 {
t.Error("GetInvoices() returned no invoices. Are you testing with an empty Harvest account?")
}
invoice, err := api.GetInvoice(invoices[0].ID, Defaults())
if err != nil {
t.Error(err)
}
if invoice.Amount <= 0 {
t.Error("Invoice should have an amount.")
}
messages, err := api.GetInvoiceMessages(invoice.ID, Defaults())
if err != nil {
t.Error(err)
}
if len(messages) < 1 {
t.Skipf("Invoice %d had no messages, which might signal a problem, or that it doesn't have any messages.", invoice.ID)
}
}
func TestRealWorldGetUsers(t *testing.T) {
api := realWorldTestAPI(t)
users, err := api.GetUsers(Defaults())
if err != nil {
t.Error(err)
}
if len(users) < 1 {
t.Error("GetUsers() returned no users. Are you testing with an empty Harvest account?")
}
user, err := api.GetUser(users[0].ID, Defaults())
if err != nil {
t.Error(err)
}
if user.FirstName == "" {
t.Error("User FirstName was blank")
}
}
func TestRealWorldGetEstimates(t *testing.T) {
api := realWorldTestAPI(t)
estimates, err := api.GetEstimates(Defaults())
if err != nil {
t.Error(err)
}
if len(estimates) < 1 {
t.Error("GetEstimates() returned no estimates. Are you testing with an empty Harvest account?")
}
estimate, err := api.GetEstimate(estimates[0].ID, Defaults())
if err != nil {
t.Error(err)
}
if estimate.Subject == "" {
t.Error("Retrieved estimate was missing a subject.")
}
}
/*
func TestCreateUpdateDeleteProject(t *testing.T) {
api := realWorldTestAPI(t)
clients, err := api.GetClients(Defaults())
if err != nil {
t.Fatal(err)
}
p := Project{
Name: "Test Harvest API Project from github.com/adlio/harvest",
ClientID: clients[0].ID,
Active: true,
BillBy: "Project",
}
// Create Project
err = api.CreateProject(&p, Defaults())
if err != nil {
t.Fatal(err)
}
if p.ID == 0 {
t.Errorf("Project %s should not have Zero ID", p.Name)
}
// Now make a change
p.StartsOn = Date{time.Now()}
p.EndsOn = Date{time.Now().AddDate(0, 0, 1)}
p.Notes = "These are the automatically-generated notes from the library."
err = api.UpdateProject(&p, Defaults())
if err != nil {
t.Fatal(err)
}
// Re-fetch to verify the update
p2, err := api.GetProject(p.ID, Defaults())
if err != nil {
t.Fatal(err)
}
if p2.Notes != "These are the automatically-generated notes from the library." {
t.Errorf("UpdateProject() seemed to have failed to change the notes on Project %d", p2.ID)
}
// Delete all the UserAssignments.. this avoids a mass-email about the delete
// to all attached staff.
assignments, err := api.GetUserAssignments(p.ID, Defaults())
for i, ua := range assignments {
if i == 0 {
ua.IsProjectManager = true
api.UpdateUserAssignment(ua, Defaults())
}
api.DeleteUserAssignment(ua, Defaults())
}
// Clean Up Our Mess
err = api.DeleteProject(&p, Defaults())
if err != nil {
t.Fatal(err)
}
}
*/
func realWorldTestAPI(t *testing.T) *API {
realworld := os.Getenv("HARVEST_REALWORLD")
account := os.Getenv("HARVEST_ACCOUNT_ID")
token := os.Getenv("HARVEST_TOKEN")
if realworld == "true" && account != "" && token != "" {
return HarvestClient(account, token)
} else {
t.Skipf("Skipping realworld tests because HARVEST_REALWORLD != true or HARVEST_ACCOUNT_ID / HARVEST_TOKEN not supplied as environment variables.")
return nil
}
}