Skip to content

Commit 3bc725f

Browse files
committed
added plan + fixed usage reports (always include)
1 parent e3d00bd commit 3bc725f

File tree

4 files changed

+50
-1
lines changed

4 files changed

+50
-1
lines changed

src/authorize_response.coffee

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,13 @@ class AuthorizeResponse extends Response
2222

2323
constructor: (@usage_reports) ->
2424
@usage_reports = []
25+
@plan = null
2526

2627

2728
add_usage_reports: (options) ->
2829
@usage_reports.push new UsageReport options
30+
31+
set_plan: (@plan) ->
2932

3033

3134
module.exports = exports = AuthorizeResponse

src/client.coffee

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,14 +352,15 @@ module.exports = class Client
352352
response = new AuthorizeResponse()
353353
authorize = doc.status.authorized
354354
plan = doc.status.plan
355+
usage_reports = doc.status.usage_reports
355356

356357
if authorize is 'true'
357358
response.success(status_code)
358359
else
359360
reason = doc.status.reason
360361
response.error(status_code, reason)
361362

362-
usage_reports = doc.status.usage_reports
363+
response.set_plan plan
363364

364365
if usage_reports
365366
for index, usage_report of usage_reports
@@ -375,6 +376,7 @@ module.exports = class Client
375376
report.period_end = usage_report.period_end
376377

377378
response.add_usage_reports report
379+
378380
callback(response)
379381

380382
_build_error_response: (status_code, xml, callback) ->

test/authorize_response_test.coffee

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,14 @@ describe 'Basic test for the 3Scale::AuthorizeResponse', ->
2121
assert.equal authorize_response.usage_reports.length, 0
2222
authorize_response.add_usage_reports usage_report_options
2323
assert.equal authorize_response.usage_reports.length, 1
24+
25+
it 'should have a set_plan method', ->
26+
authorize_response = new AuthorizeResponse()
27+
assert.equal typeof authorize_response.set_plan, 'function'
28+
29+
it 'should be able to set a plan', ->
30+
authorize_response = new AuthorizeResponse()
31+
assert.equal authorize_response.plan, null
32+
plan = 'Ultimate'
33+
authorize_response.set_plan plan
34+
assert.equal authorize_response.plan, plan

test/usage_report_test.coffee

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,37 @@ Client = require('../src/client')
55

66
describe 'Usage report tests for 3Scale::Client', ->
77
it 'should parse the response of the Authorize call', (done) ->
8+
xml_body = "<status>\
9+
<authorized>true</authorized>\
10+
<plan>Ultimate</plan>\
11+
<usage_reports>\
12+
<usage_report metric=\"hits\" period=\"day\" exceeded=\"false\">\
13+
<period_start>2010-04-26 00:00:00 +0000</period_start>\
14+
<period_end>2010-04-27 00:00:00 +0000</period_end>\
15+
<current_value>10002</current_value>\
16+
<max_value>50000</max_value>\
17+
</usage_report>\
18+
</usage_reports>\
19+
</status>"
20+
21+
nock('https://su1.3scale.net')
22+
.get('/transactions/authorize.xml?app_id=foo&provider_key=1234abcd')
23+
.reply(200, xml_body, { 'Content-Type': 'application/xml' })
24+
25+
client = new Client '1234abcd'
26+
client.authorize { app_id: 'foo' }, (response) ->
27+
assert.equal response.is_success(), true
28+
assert.equal response.status_code, 200
29+
assert.equal response.plan, 'Ultimate'
30+
assert.equal response.usage_reports[0].metric, 'hits'
31+
assert.equal response.usage_reports[0].period, 'day'
32+
assert.equal response.usage_reports[0].period_start, '2010-04-26 00:00:00 +0000'
33+
assert.equal response.usage_reports[0].period_end, '2010-04-27 00:00:00 +0000'
34+
assert.equal response.usage_reports[0].current_value, '10002'
35+
assert.equal response.usage_reports[0].max_value, '50000'
36+
done()
37+
38+
it 'should parse the response of the Authorize call, when limit is exceeded', (done) ->
839
xml_body = "<status>\
940
<authorized>false</authorized>\
1041
<reason>usage limits are exceeded</reason>\
@@ -28,6 +59,7 @@ describe 'Usage report tests for 3Scale::Client', ->
2859
assert.equal response.is_success(), false
2960
assert.equal response.status_code, 409
3061
assert.equal response.error_message, 'usage limits are exceeded'
62+
assert.equal response.plan, 'Ultimate'
3163
assert.equal response.usage_reports[0].metric, 'hits'
3264
assert.equal response.usage_reports[0].period, 'day'
3365
assert.equal response.usage_reports[0].period_start, '2010-04-26 00:00:00 +0000'
@@ -58,6 +90,7 @@ describe 'Usage report tests for 3Scale::Client', ->
5890
assert.equal response.is_success(), false
5991
assert.equal response.status_code, 409
6092
assert.equal response.error_message, 'usage limits are exceeded'
93+
assert.equal response.plan, 'Ultimate'
6194
assert.equal response.usage_reports[0].metric, 'hits'
6295
assert.equal response.usage_reports[0].period, 'eternity'
6396
assert.equal response.usage_reports[0].period_start, undefined

0 commit comments

Comments
 (0)