-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathusage_report_test.coffee
146 lines (130 loc) · 7.01 KB
/
usage_report_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
138
139
140
141
142
143
144
145
146
assert = require 'assert'
nock = require 'nock'
Client = require('../src/client')
describe 'Usage report tests for 3Scale::Client', ->
it 'should parse the response of the Authorize call', (done) ->
xml_body = "<status>\
<authorized>true</authorized>\
<plan>Ultimate</plan>\
<usage_reports>\
<usage_report metric=\"hits\" period=\"day\" exceeded=\"false\">\
<period_start>2010-04-26 00:00:00 +0000</period_start>\
<period_end>2010-04-27 00:00:00 +0000</period_end>\
<current_value>10002</current_value>\
<max_value>50000</max_value>\
</usage_report>\
</usage_reports>\
</status>"
nock('https://su1.3scale.net')
.get('/transactions/authorize.xml?app_id=foo&provider_key=1234abcd')
.reply(200, xml_body, { 'Content-Type': 'application/xml' })
client = new Client '1234abcd'
client.authorize { app_id: 'foo' }, (response) ->
assert.equal response.is_success(), true
assert.equal response.status_code, 200
assert.equal response.plan, 'Ultimate'
assert.equal response.usage_reports[0].metric, 'hits'
assert.equal response.usage_reports[0].period, 'day'
assert.equal response.usage_reports[0].period_start, '2010-04-26 00:00:00 +0000'
assert.equal response.usage_reports[0].period_end, '2010-04-27 00:00:00 +0000'
assert.equal response.usage_reports[0].current_value, '10002'
assert.equal response.usage_reports[0].max_value, '50000'
done()
it 'should parse the response of the Authorize call, when limit is exceeded', (done) ->
xml_body = "<status>\
<authorized>false</authorized>\
<reason>usage limits are exceeded</reason>\
<plan>Ultimate</plan>\
<usage_reports>\
<usage_report metric=\"hits\" period=\"day\" exceeded=\"true\">\
<period_start>2010-04-26 00:00:00 +0000</period_start>\
<period_end>2010-04-27 00:00:00 +0000</period_end>\
<current_value>50002</current_value>\
<max_value>50000</max_value>\
</usage_report>\
</usage_reports>\
</status>"
nock('https://su1.3scale.net')
.get('/transactions/authorize.xml?app_id=foo&provider_key=1234abcd')
.reply(409, xml_body, { 'Content-Type': 'application/xml' })
client = new Client '1234abcd'
client.authorize { app_id: 'foo' }, (response) ->
assert.equal response.is_success(), false
assert.equal response.status_code, 409
assert.equal response.error_message, 'usage limits are exceeded'
assert.equal response.plan, 'Ultimate'
assert.equal response.usage_reports[0].metric, 'hits'
assert.equal response.usage_reports[0].period, 'day'
assert.equal response.usage_reports[0].period_start, '2010-04-26 00:00:00 +0000'
assert.equal response.usage_reports[0].period_end, '2010-04-27 00:00:00 +0000'
assert.equal response.usage_reports[0].current_value, '50002'
assert.equal response.usage_reports[0].max_value, '50000'
done()
it 'should parse the response of the Authorize call with eternity limits', (done) ->
xml_body = "<status>\
<authorized>false</authorized>\
<reason>usage limits are exceeded</reason>\
<plan>Ultimate</plan>\
<usage_reports>\
<usage_report metric=\"hits\" period=\"eternity\" exceeded=\"true\">\
<current_value>50002</current_value>\
<max_value>50000</max_value>\
</usage_report>\
</usage_reports>\
</status>"
nock('https://su1.3scale.net')
.get('/transactions/authorize.xml?app_id=foo&provider_key=1234abcd')
.reply(409, xml_body, { 'Content-Type': 'application/xml' })
client = new Client '1234abcd'
client.authorize { app_id: 'foo' }, (response) ->
assert.equal response.is_success(), false
assert.equal response.status_code, 409
assert.equal response.error_message, 'usage limits are exceeded'
assert.equal response.plan, 'Ultimate'
assert.equal response.usage_reports[0].metric, 'hits'
assert.equal response.usage_reports[0].period, 'eternity'
assert.equal response.usage_reports[0].period_start, undefined
assert.equal response.usage_reports[0].period_end, undefined
assert.equal response.usage_reports[0].current_value, '50002'
assert.equal response.usage_reports[0].max_value, '50000'
done()
it 'should parse the response of the Authorize call with multiple usage_report entries', (done) ->
xml_body = "<status>\
<authorized>true</authorized>\
<plan>Ultimate</plan>\
<usage_reports>\
<usage_report metric=\"hits\" period=\"day\" exceeded=\"false\">\
<period_start>2010-04-26 00:00:00 +0000</period_start>\
<period_end>2010-04-27 00:00:00 +0000</period_end>\
<current_value>10002</current_value>\
<max_value>50000</max_value>\
</usage_report>\
<usage_report metric=\"misses\" period=\"day\" exceeded=\"false\">\
<period_start>2010-04-26 00:00:00 +0000</period_start>\
<period_end>2010-04-27 00:00:00 +0000</period_end>\
<current_value>12</current_value>\
<max_value>50000</max_value>\
</usage_report>\
</usage_reports>\
</status>"
nock('https://su1.3scale.net')
.get('/transactions/authorize.xml?app_id=foo&provider_key=1234abcd')
.reply(200, xml_body, { 'Content-Type': 'application/xml' })
client = new Client '1234abcd'
client.authorize { app_id: 'foo' }, (response) ->
assert.equal response.is_success(), true
assert.equal response.status_code, 200
assert.equal response.plan, 'Ultimate'
assert.equal response.usage_reports[0].metric, 'hits'
assert.equal response.usage_reports[0].period, 'day'
assert.equal response.usage_reports[0].period_start, '2010-04-26 00:00:00 +0000'
assert.equal response.usage_reports[0].period_end, '2010-04-27 00:00:00 +0000'
assert.equal response.usage_reports[0].current_value, '10002'
assert.equal response.usage_reports[0].max_value, '50000'
assert.equal response.usage_reports[1].metric, 'misses'
assert.equal response.usage_reports[1].period, 'day'
assert.equal response.usage_reports[1].period_start, '2010-04-26 00:00:00 +0000'
assert.equal response.usage_reports[1].period_end, '2010-04-27 00:00:00 +0000'
assert.equal response.usage_reports[1].current_value, '12'
assert.equal response.usage_reports[1].max_value, '50000'
done()