-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.js
141 lines (113 loc) · 4 KB
/
tests.js
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
/* Copyright Spurwing.io and Healthie Inc.
* Released under the MIT license
* https://www.spurwing.io/
*/
// chai testing framework: https://www.chaijs.com/guide/styles/
const chai = require('chai');
chai.should();
const expect = chai.expect;
let PID, KEY;
if (process.env.SPURWING_PID && process.env.SPURWING_KEY) {
PID = process.env.SPURWING_PID;
KEY = process.env.SPURWING_KEY;
} else {
let CONFIG = require('./config.js');
PID = CONFIG.provider_id;
KEY = CONFIG.api_key;
}
if (!PID || !KEY) throw 'missing PID and/or KEY values';
const Spurwing = require('./spurwing.js')
async function runner(testname, func) {
try {
console.log(testname, 'STARTED')
await func()
console.log('\n'+testname, 'PASSED\n')
} catch (err) {
console.error(testname, 'FAILED')
console.error(err)
process.exit(1); // Exit the proccess if failed
}
}
function logResp(obj) {
// console.log(obj)
process.stdout.write(".");
}
(async () => {
await runner('TEST 1', (async() => {
let sp = new Spurwing();
let A = await sp.get_appointment_types(PID)
logResp({A})
expect(A.length).to.be.at.least(1)
let appointment_type_id = A[0].id;
let B = await sp.get_days_available(PID, appointment_type_id)
logResp({B})
expect(B.days_available.length).to.be.at.least(1) // at least one day available this month
let C = await sp.get_slots_available(PID, appointment_type_id, dateNow(), dateTomorrow())
logResp({C})
expect(C.slots_available.length).to.be.at.least(10) // each day has 96 15min slots (60*24 / 15 == 96)
let slot = C.slots_available[5].date
let D = await sp.complete_booking(PID, appointment_type_id, '[email protected]', 'Ilya', 'Nevo', slot, 'Test booking');
logResp({D})
D.should.have.property('appointment')
expect(D.appointment.length).to.equal(60)
let E = await sp.list_appointments(KEY, 1000, 0)
logResp({E})
E.should.have.property('data')
E.data.should.have.property('appointments')
expect(E.data.appointmentsCount).to.be.at.least(1)
let apid = D.appointment.id;
let F = await sp.delete_appointment(KEY, apid)
logResp({F})
F.should.have.property('data')
F.data.should.have.property('appointment')
F.data.appointment.id.should.equal(D.appointment.id)
F.errors.should.have.lengthOf(0)
}));
await runner('TEST 2', (async() => {
let sp = new Spurwing();
let A = await sp.list_appointments(KEY, 1000, 0)
logResp({A})
A.should.have.property('data')
A.data.should.have.property('appointments')
}));
await runner('TEST 3', (async() => {
let sp = new Spurwing();
let A = await sp.get_appointment_types(PID)
logResp({A})
expect(A.length).to.be.at.least(1)
let appointment_type_id = A[3].id;
let B = await sp.create_group_appointment(KEY, PID, appointment_type_id, dateTomorrow() + ' 16:00:00')
logResp({B})
B.should.have.property('data')
B.data.should.have.property('appointment')
B.data.appointment.should.have.property('id')
let apid = B.data.appointment.id
async function add_attendee(fn, ln, email) {
let D = await sp.complete_booking(PID, appointment_type_id, email, fn, ln, null, null, apid);
logResp({D})
D.should.have.property('appointment')
}
await add_attendee('john', 'G', '[email protected]')
await add_attendee('bill', 'H', '[email protected]')
let E = await sp.list_appointments(KEY, 1000, 0)
logResp({E})
let F = await sp.delete_appointment(KEY, apid)
logResp({F})
F.should.have.property('data')
F.data.should.have.property('appointment')
F.data.appointment.id.should.equal(apid)
F.errors.should.have.lengthOf(0)
}));
})();
//////////////////////////////
////// helper functions //////
//////////////////////////////
function dateNow() {
let d = new Date();
return d.getFullYear() + '-' + (d.getMonth()+1) + '-' + d.getDate();
}
function dateTomorrow() {
let d = new Date();
d.setDate(d.getDate() + 1);
return d.getFullYear() + '-' + (d.getMonth()+1) + '-' + d.getDate();
}