· 6 years ago · Jun 30, 2019, 11:24 AM
1const chai = require('chai')
2const chaiHttp = require('chai-http')
3const jwt = require('jsonwebtoken')
4const server = require('../app')
5
6chai.should()
7chai.use(chaiHttp)
8
9const endpointapartments = '/api/apartments'
10const endpointreservaton = '/api/apartments/1232/reservations'
11const authorizationHeader = 'Authorization'
12let token
13
14//
15// Deze functie wordt voorafgaand aan alle tests 1 x uitgevoerd.
16//
17before(() => {
18 console.log('- before: get valid token')
19 // We hebben een token nodig om een ingelogde gebruiker te simuleren.
20 // Hier kiezen we ervoor om een token voor UserId 1 te gebruiken.
21 const payload = {
22 UserId: 1
23 }
24 jwt.sign({ data: payload }, 'secretkey', { expiresIn: 2 * 60 }, (err, result) => {
25 if (result) {
26 token = result
27 }
28 })
29})
30
31describe('Apartments API POST', () => {
32 it('should return a valid id when posting a valid object', done => {
33 chai
34 .request(server)
35 .post(endpointapartments)
36 .set(authorizationHeader, 'Bearer ' + token)
37 .send({
38 description: 'description',
39 streetaddress: 'streetaddress',
40 postalcode: '3388AB',
41 city: 'testing'
42 })
43 .end((err, res) => {
44 res.should.have.status(201)
45 res.body.should.be.a('object')
46
47 const result = res.body.result
48 const apartment = result[0]
49
50 apartment.should.have.property('StreetAddress')
51 apartment.should.have.property('Description')
52 apartment.should.have.property('PostalCode')
53 apartment.should.not.have.property('Password')
54 done()
55 })
56 })
57})
58
59describe('Apartments API POST NO HEADER', () => {
60 it('Should return an error. ', done => {
61 chai
62 .request(server)
63 .post(endpointapartments)
64 .set(authorizationHeader, 'Bearer ')
65 .send({
66 description: 'description',
67 streetaddress: 'streetaddress',
68 postalcode: '3388AB',
69 city: 'testing'
70 })
71 .end((err, res) => {
72 res.should.have.status(401)
73 done()
74 })
75 })
76 })
77
78describe('Apartment API GET', () => {
79 it('should return an array of Apartments', done => {
80 chai
81 .request(server)
82 .get(endpointapartments)
83 .end((err, res) => {
84 res.should.have.status(200)
85 res.body.should.be.a('object')
86
87 const result = res.body.result
88 result.should.be.an('array')
89 const apartment = result[0]
90
91 apartment.should.have.property('StreetAddress')
92 apartment.should.have.property('Description')
93 apartment.should.have.property('PostalCode')
94 apartment.should.not.have.property('Password')
95 done()
96 })
97 })
98})
99describe('Apartment API GET', () => {
100 it('should return an array of Apartments', done => {
101 chai
102 .request(server)
103 .get(endpointapartments)
104 .end((err, res) => {
105 res.should.have.status(200)
106 res.body.should.be.a('object')
107
108 const result = res.body.result
109 result.should.be.an('array')
110 const apartment = result[0]
111
112 apartment.should.have.property('StreetAddress')
113 apartment.should.have.property('Description')
114 apartment.should.have.property('PostalCode')
115 done()
116 })
117 })
118})
119describe('Reservation API POST', () => {
120 it('should return a correct code', done => {
121 chai
122 .request(server)
123 .post(endpointreservaton)
124 .set(authorizationHeader, 'Bearer ' + token)
125 .send({
126 status: 'Initial',
127 startdate: '2010-2-2',
128 enddate: '2012-2-1'
129 })
130 .end((err, res) => {
131 res.should.have.status(201)
132 res.body.should.be.a('object')
133
134 const result = res.body.result
135 result.should.be.an('array')
136 done()
137 })
138 })
139})
140describe('Reservations API GET', () => {
141 it('should return an array of Reservations', done => {
142 chai
143 .request(server)
144 .get(endpointreservaton)
145 .end((err, res) => {
146 res.should.have.status(200)
147 res.body.should.be.a('object')
148
149 const result = res.body.result
150 result.should.be.an('array')
151 const apartment = result[0]
152
153 apartment.should.have.property('ReservationId')
154 apartment.should.have.property('ApartmentId')
155 apartment.should.have.property('StartDate')
156 apartment.should.have.property('EndDate')
157 apartment.should.have.property('UserId')
158 apartment.should.have.property('Status')
159
160 done()
161 })
162 })
163 })