|
1
|
|
|
require 'spec_helper.rb' |
|
2
|
|
|
require 'watchdog_server' |
|
3
|
|
|
require 'json' |
|
4
|
|
|
|
|
5
|
|
|
def app |
|
6
|
|
|
WatchDogServer |
|
7
|
|
|
end |
|
8
|
|
|
|
|
9
|
|
|
def test_user |
|
10
|
|
|
user = Hash.new |
|
11
|
|
|
user['email'] = '[email protected]' |
|
12
|
|
|
user['name'] = 'Foo Bar' |
|
13
|
|
|
user['org'] = 'Baz B.V.' |
|
14
|
|
|
user['org_website'] = 'http://baz.nl' |
|
15
|
|
|
user['prize'] = false |
|
16
|
|
|
user['programmingExperience'] = '1-2 years' |
|
17
|
|
|
user |
|
18
|
|
|
end |
|
19
|
|
|
|
|
20
|
|
|
def empty_user |
|
21
|
|
|
user = Hash.new |
|
22
|
|
|
user['programmingExperience'] = nil |
|
23
|
|
|
user |
|
24
|
|
|
end |
|
25
|
|
|
|
|
26
|
|
|
existing_user = nil |
|
27
|
|
|
existing_project = nil |
|
28
|
|
|
|
|
29
|
|
|
def test_project(user_id) |
|
30
|
|
|
project = Hash.new |
|
31
|
|
|
project['name'] = 'Foo Bar Proj' |
|
32
|
|
|
project['role'] = 'Foo Barer' |
|
33
|
|
|
project['belongToASingleSofware'] = true |
|
34
|
|
|
project['usesJunit'] = true |
|
35
|
|
|
project['usesOtherFrameworks'] = false |
|
36
|
|
|
project['productionPercentage'] = 50 |
|
37
|
|
|
project['useJunitOnlyForUnitTesting'] = false |
|
38
|
|
|
project['followTestDrivenDesign'] = false |
|
39
|
|
|
project['userId'] = user_id |
|
40
|
|
|
project |
|
41
|
|
|
end |
|
42
|
|
|
|
|
43
|
|
|
def test_interval(from, to) |
|
44
|
|
|
interval = Hash.new |
|
45
|
|
|
interval['ts'] = from |
|
46
|
|
|
interval['te'] = to |
|
47
|
|
|
interval |
|
48
|
|
|
end |
|
49
|
|
|
|
|
50
|
|
|
def test_event(timestamp) |
|
51
|
|
|
event = Hash.new |
|
52
|
|
|
event['ts'] = timestamp |
|
53
|
|
|
event |
|
54
|
|
|
end |
|
55
|
|
|
|
|
56
|
|
|
describe 'The WatchDog Server' do |
|
57
|
|
|
|
|
58
|
|
|
before(:each) do |
|
59
|
|
|
mongo = WatchDogServer.new.helpers.mongo |
|
60
|
|
|
mongo.close |
|
61
|
|
|
end |
|
62
|
|
|
|
|
63
|
|
|
it 'should woof' do |
|
64
|
|
|
get '/' |
|
65
|
|
|
expect(last_response).to be_ok |
|
66
|
|
|
expect(last_response.body).to eq('Woof Woof') |
|
67
|
|
|
end |
|
68
|
|
|
|
|
69
|
|
|
it 'should get client version' do |
|
70
|
|
|
get '/client' |
|
71
|
|
|
expect(last_response).to be_ok |
|
72
|
|
|
expect(last_response.body).to eq('"1.7.0"') |
|
73
|
|
|
end |
|
74
|
|
|
|
|
75
|
|
|
it 'should create a user when the details are correct' do |
|
76
|
|
|
post '/user', test_user.to_json |
|
77
|
|
|
|
|
78
|
|
|
last_response.status.should eql(201) |
|
79
|
|
|
expect(last_response.body).to match(/^[0-9a-z]{40}$/) |
|
80
|
|
|
existing_user = last_response.body |
|
81
|
|
|
end |
|
82
|
|
|
|
|
83
|
|
|
it 'should return 400 on bad JSON request to /user' do |
|
84
|
|
|
post '/user', 'foobar' |
|
85
|
|
|
last_response.status.should eql(400) |
|
86
|
|
|
end |
|
87
|
|
|
|
|
88
|
|
|
it 'should create a project when the details are correct' do |
|
89
|
|
|
post '/project', test_project(existing_user).to_json |
|
90
|
|
|
|
|
91
|
|
|
last_response.status.should eql(201) |
|
92
|
|
|
expect(last_response.body).to match(/^[0-9a-z]{40}$/) |
|
93
|
|
|
existing_project = last_response.body |
|
94
|
|
|
end |
|
95
|
|
|
|
|
96
|
|
|
it 'should return 400 on bad JSON request to /project' do |
|
97
|
|
|
post '/project', 'foobar' |
|
98
|
|
|
last_response.status.should eql(400) |
|
99
|
|
|
end |
|
100
|
|
|
|
|
101
|
|
|
it 'should not create a project when the user does not exist' do |
|
102
|
|
|
post '/project', test_project(nil).to_json |
|
103
|
|
|
|
|
104
|
|
|
last_response.status.should eql(404) |
|
105
|
|
|
end |
|
106
|
|
|
|
|
107
|
|
|
it 'should return 400 on bad JSON to /users/:id/intervals' do |
|
108
|
|
|
post '/user/foobar/foobarproject/intervals', 'foobar' |
|
109
|
|
|
last_response.status.should eql(400) |
|
110
|
|
|
end |
|
111
|
|
|
|
|
112
|
|
|
it 'should return 400 on non JSON array being sent to /users/:id/intervals' do |
|
113
|
|
|
post '/user/foobar/foobarproject/intervals', '{"foo":"bar"}' |
|
114
|
|
|
last_response.status.should eql(400) |
|
115
|
|
|
end |
|
116
|
|
|
|
|
117
|
|
|
it 'should return 400 when negative intervals exist' do |
|
118
|
|
|
intervals = (1..10).map{|x| test_interval(x + 1, x)} |
|
119
|
|
|
|
|
120
|
|
|
post '/user/foobar/foobarproj/intervals', intervals.to_json |
|
121
|
|
|
last_response.status.should eql(400) |
|
122
|
|
|
end |
|
123
|
|
|
|
|
124
|
|
|
it 'should return 400 on bad JSON to /users/:id/:pid/events' do |
|
125
|
|
|
post '/user/foobar/foobarproject/events', 'foobar' |
|
126
|
|
|
last_response.status.should eql(400) |
|
127
|
|
|
end |
|
128
|
|
|
|
|
129
|
|
|
it 'should return 400 on non JSON array being sent to /users/:id/:pid/events' do |
|
130
|
|
|
post '/user/foobar/foobarproject/events', '{"foo":"bar"}' |
|
131
|
|
|
last_response.status.should eql(400) |
|
132
|
|
|
end |
|
133
|
|
|
|
|
134
|
|
|
it 'should return 404 for non-existing user' do |
|
135
|
|
|
get '/user/noexistingfoobar' |
|
136
|
|
|
last_response.status.should eql(404) |
|
137
|
|
|
end |
|
138
|
|
|
|
|
139
|
|
|
it 'should return 200 for existing user' do |
|
140
|
|
|
get '/user/' + existing_user |
|
141
|
|
|
last_response.status.should eql(200) |
|
142
|
|
|
end |
|
143
|
|
|
|
|
144
|
|
|
it 'should return 200 for existing project' do |
|
145
|
|
|
get '/project/' + existing_project |
|
146
|
|
|
last_response.status.should eql(200) |
|
147
|
|
|
end |
|
148
|
|
|
|
|
149
|
|
|
it 'should return 404 when posting intervals for non-existing user' do |
|
150
|
|
|
intervals = (1..10).map{|x| test_interval(x, x + 1)} |
|
151
|
|
|
|
|
152
|
|
|
post '/user/foobar/foobarprojects/intervals', intervals.to_json |
|
153
|
|
|
last_response.status.should eql(404) |
|
154
|
|
|
end |
|
155
|
|
|
|
|
156
|
|
|
it 'should return 404 when posting intervals for non-existing project' do |
|
157
|
|
|
intervals = (1..10).map{|x| test_interval(x, x + 1)} |
|
158
|
|
|
|
|
159
|
|
|
post '/user/' + existing_user + '/intervals', intervals.to_json |
|
160
|
|
|
last_response.status.should eql(404) |
|
161
|
|
|
end |
|
162
|
|
|
|
|
163
|
|
|
it 'should return 404 when posting events for non-existing user' do |
|
164
|
|
|
events = (1..10).map{|x| test_event(x)} |
|
165
|
|
|
|
|
166
|
|
|
post '/user/noexistingfoobar/foobarprojects/events', events.to_json |
|
167
|
|
|
last_response.status.should eql(404) |
|
168
|
|
|
end |
|
169
|
|
|
|
|
170
|
|
|
it 'should return 404 when posting events for non-existing project' do |
|
171
|
|
|
events = (1..10).map{|x| test_event(x)} |
|
172
|
|
|
|
|
173
|
|
|
post '/user/' + existing_user + '/noexistingfoobarproject/events', events.to_json |
|
174
|
|
|
last_response.status.should eql(404) |
|
175
|
|
|
end |
|
176
|
|
|
|
|
177
|
|
|
it 'should return 404 when trying to register a user with missing programming experience' do |
|
178
|
|
|
post '/user', empty_user.to_json |
|
179
|
|
|
last_response.status.should eql(404) |
|
180
|
|
|
end |
|
181
|
|
|
|
|
182
|
|
|
it 'should return the number of stored intervals on successful insert' do |
|
183
|
|
|
intervals = (1..10).map{|x| test_interval(x, x + 1)} |
|
184
|
|
|
user = test_user |
|
185
|
|
|
post '/user', user.to_json |
|
186
|
|
|
user_id = last_response.body |
|
187
|
|
|
project = test_project(user_id) |
|
188
|
|
|
post '/project', project.to_json |
|
189
|
|
|
project_id = last_response.body |
|
190
|
|
|
|
|
191
|
|
|
post "/user/#{user_id}/#{project_id}/intervals", intervals.to_json |
|
192
|
|
|
last_response.status.should eql(201) |
|
193
|
|
|
expect(last_response.body).to eq('10') |
|
194
|
|
|
end |
|
195
|
|
|
|
|
196
|
|
|
it 'should return the number of stored events on successful insert' do |
|
197
|
|
|
events = (1..10).map{|x| test_event(x)} |
|
198
|
|
|
user = test_user |
|
199
|
|
|
post '/user', user.to_json |
|
200
|
|
|
user_id = last_response.body |
|
201
|
|
|
project = test_project(user_id) |
|
202
|
|
|
post '/project', project.to_json |
|
203
|
|
|
project_id = last_response.body |
|
204
|
|
|
|
|
205
|
|
|
post "/user/#{user_id}/#{project_id}/events", events.to_json |
|
206
|
|
|
last_response.status.should eql(201) |
|
207
|
|
|
expect(last_response.body).to eq('10') |
|
208
|
|
|
end |
|
209
|
|
|
|
|
210
|
|
|
end |
|
211
|
|
|
|