Validate coupon code for subscriptions fixes #8398 (#8440)

* (fix) If coupon is not valid, display an error

* (test) Add valid and invalid coupon tests for applyCoupon

* Add test descriptions
This commit is contained in:
Cai Lu
2017-01-23 11:01:30 -08:00
committed by Keith Holliday
parent 79c3efaf9c
commit 79b51a40ce
2 changed files with 62 additions and 5 deletions

View File

@@ -1,7 +1,7 @@
'use strict';
describe('Settings Controller', function () {
var rootScope, scope, user, User, ctrl;
var rootScope, scope, $httpBackend, user, User, ctrl, Notification;
const actionClickEvent = {
target: document.createElement('button'),
@@ -29,18 +29,27 @@ describe('Settings Controller', function () {
releaseBoth: sandbox.stub(),
};
Notification = {
error: sandbox.stub(),
text: sandbox.stub()
};
$provide.value('Notification', Notification);
$provide.value('User', User);
$provide.value('Guide', sandbox.stub());
});
inject(function(_$rootScope_, _$controller_) {
inject(function(_$rootScope_, _$controller_, _$httpBackend_) {
scope = _$rootScope_.$new();
rootScope = _$rootScope_;
$httpBackend = _$httpBackend_;
$httpBackend.whenGET(/partials/).respond();
// Load RootCtrl to ensure shared behaviors are loaded
_$controller_('RootCtrl', {$scope: scope, User: User});
_$controller_('RootCtrl', {$scope: scope, User: User, Notification: Notification});
ctrl = _$controller_('SettingsCtrl', {$scope: scope, User: User});
ctrl = _$controller_('SettingsCtrl', {$scope: scope, User: User, Notification: Notification});
});
});
@@ -281,4 +290,48 @@ describe('Settings Controller', function () {
});
});
});
context('Validating coupons', function () {
describe('#applyCoupon', function () {
it('displays an error when an invalid coupon is applied', function () {
$httpBackend
.whenPOST('/api/v3/coupons/validate/INVALID_COUPON?userV=undefined')
.respond(200, {
success: true,
data: {
valid: false
},
notifications: [],
userV: 'undefined'
});
scope.applyCoupon('INVALID_COUPON');
$httpBackend.flush();
expect(Notification.error).to.be.called;
expect(Notification.error).to.be.calledWith(env.t('invalidCoupon'), true);
});
it('displays an confirmation when a valid coupon is applied', function () {
$httpBackend
.whenPOST('/api/v3/coupons/validate/VALID_COUPON?userV=undefined')
.respond(200, {
success: true,
data: {
valid: true
},
notifications: [],
userV: 'undefined'
});
scope.applyCoupon('VALID_COUPON');
$httpBackend.flush();
expect(Notification.error).to.not.be.called;
expect(Notification.text).to.be.calledWith('Coupon applied!');
});
});
});
});

View File

@@ -269,7 +269,11 @@ habitrpg.controller('SettingsCtrl',
$scope.applyCoupon = function(coupon){
$http.post(ApiUrl.get() + '/api/v3/coupons/validate/'+coupon)
.success(function(){
.success(function(res, code){
if (!res.data.valid) {
Notification.error(env.t('invalidCoupon'), true);
return;
}
Notification.text("Coupon applied!");
var subs = Content.subscriptionBlocks;
subs["basic_6mo"].discount = true;