-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.test.coffee
140 lines (114 loc) · 5.78 KB
/
app.test.coffee
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
assert = require('assert')
#request = require('request')
logmeup = require('../../app/logmeup')
config = require('../../config/config')
require('string')
request2 = require('superagent')
T = (v) -> assert(v)
F = (v) -> assert(!v)
config.port = 7171
describe 'LogMeUp', ->
collection = 'gitpilot' + Date.now()
app = 'servermonitor' + Date.now()
beforeEach (done) ->
logmeup.init config: config, (err) ->
T err is null
logmeup.app.listen(config.port)
request2.del("http://localhost:#{config.port}/log/#{collection}/#{app}").end -> done()
afterEach (done) ->
logmeup.app.close()
done()
describe 'when PUT /log/:collection/:app', ->
it 'should create the database collection if it doesnt exist', (done) ->
request2.put("http://localhost:#{config.port}/log/#{collection}/#{app}").end (res) ->
console.log res.text
T res.text.startsWith('Created')
T res.text.endsWith(collection + '_' + app)
done()
it 'should return an error message if the database collection does exist', (done) ->
request2.put("http://localhost:#{config.port}/log/#{collection}/#{app}").end (res) ->
request2.put("http://localhost:#{config.port}/log/#{collection}/#{app}").end (res) ->
T res.text.startsWith('Error')
T res.text.contains('exists')
done()
describe 'when DELETE /log/:collection/:app', ->
it 'should delete the database collection if it exists', (done) ->
request2.put("http://localhost:#{config.port}/log/#{collection}/#{app}").end (res) ->
T res.text.startsWith('Created')
request2.del("http://localhost:#{config.port}/log/#{collection}/#{app}").end (res) ->
#T err is null
T res.text.startsWith('Deleted')
T res.text.endsWith(collection + '_' + app)
done()
it 'should return an error message if the database collection does not exist', (done) ->
request2.del("http://localhost:#{config.port}/log/#{collection}/#{app}").end (res) ->
#T err is null
T res.text.startsWith('Error')
done()
describe 'when POST /log/:collection/:app', ->
it 'should insert the request body JSON data into the database', (done) ->
data = name: 'JP', lastName: 'Richardson'
url = "http://localhost:#{config.port}/log/#{collection}/#{app}"
request2.put(url).end (res) ->
request2.post(url).type('json').send(data).end (res) -> #application/json
#T err is null
T res.text.startsWith('Stored data')
done()
it 'should insert the request body Form data into the database', (done) ->
d = data: 'JP Richardson' #must use 'data' key
url = "http://localhost:#{config.port}/log/#{collection}/#{app}"
request2.put(url).end (res) ->
request2.post(url).set('Content-Type':'application/x-www-form-urlencoded').send(d).end (res) -> #BUG IN SUPERAGENT, in browser it's 'form-data' #application/x-www-form-urlencoded
#T err is null
T res.text.startsWith('Stored data')
done()
it 'should return an error message if the database collection does not exist', (done) ->
data = name: 'JP', lastName: 'Richardson'
url = "http://localhost:#{config.port}/log/#{collection}/#{app}"
request2.post(url).type('json').send(data).end (res) ->
#T err is null
T res.text.startsWith('Error')
T res.text.contains('does not exist')
done()
describe 'when GET /log/:collection/:app', ->
it 'should show the page that contains the log data', (done) ->
request2.put("http://localhost:#{config.port}/log/#{collection}/#{app}").end (res) ->
request2.get("http://localhost:#{config.port}/log/#{collection}/#{app}").end (res) ->
#T err is null
T res.text.contains('Log data')
done()
it 'should return an error message if the database collection does not exist', (done) ->
request2.get("http://localhost:#{config.port}/log/#{collection}/#{app}").end (res) ->
#T err is null
T res.text.startsWith('Error')
T res.text.contains('does not exist')
done()
describe 'when GET /log/:collection/:app/data.json', ->
it 'should return an error if the database collection does not exist', (done) ->
request2.get("http://localhost:#{config.port}/log/#{collection}/#{app}/data.json").end (res) ->
T res.text.startsWith('Error')
T res.text.contains('does not exist')
done()
it 'should return JSON sorted (DESC by date) records in an array with the count of the total number of records', (done) ->
insertRecord = (data,callback) -> #convenience method to insertData
url = "http://localhost:#{config.port}/log/#{collection}/#{app}"
request2.post(url).type('json').send(data).end (res) ->
#console.log body
T res.text.startsWith('Stored data')
callback()
request2.put("http://localhost:#{config.port}/log/#{collection}/#{app}").end (res) ->
RECORD_COUNT = 10
recur = (count) -> #this is so all the insertData requests don't happen simultaneously
if count < RECORD_COUNT
data = someNumber: Math.random(), someOtherValue: 'rarrr!'
insertRecord data, ->
if count < RECORD_COUNT - 1
recur(count+1)
else
request2.get("http://localhost:#{config.port}/log/#{collection}/#{app}/data.json").end (res) ->
data = res.body
T data.total is RECORD_COUNT
T data.records.length is RECORD_COUNT
T data.records[0].created_at > data.records[1].created_at > data.records[2].created_at
request2.del("http://localhost:#{config.port}/log/#{collection}/#{app}").end -> done() #delete collection
recur(0)