Commit 235656af by jaebradley Committed by Jae Bradley

add currency cookie validation

delete code
parent 80f51474
......@@ -236,10 +236,14 @@ define([
});
},
isValidLocalCurrencyCookie: function(cookie) {
return !!cookie && !!cookie.countryCode && !!cookie.symbol && !!cookie.rate && !!cookie.code;
},
addPriceDisclaimer: function() {
var price = $('#basket-total').find('> .price').text(),
countryData = Cookies.getJSON('edx-price-l10n');
if (countryData && countryData.countryCode !== 'USA') {
if (BasketPage.isValidLocalCurrencyCookie(countryData) && countryData.countryCode !== 'USA') {
$('<span>').attr('class', 'price-disclaimer')
.text('* This total contains an approximate conversion. You will be charged ' + price + ' USD.')
.appendTo('div[aria-labelledby="order-details-region"]');
......@@ -250,7 +254,7 @@ define([
var countryData = Cookies.getJSON('edx-price-l10n');
// Default to USD when the exchange rate cookie doesn't exist
if (countryData && countryData.countryCode !== 'USA') {
if (BasketPage.isValidLocalCurrencyCookie(countryData) && countryData.countryCode !== 'USA') {
return countryData.symbol + Math.round(priceInUsd * countryData.rate) + ' '
+ countryData.code + ' *';
} else {
......
......@@ -733,7 +733,8 @@ define([
Cookies.remove(EDX_PRICE_LOCATION_COOKIE_NAME);
});
it('should not add disclaimer when cookie does not exist', function() {
it('should not add disclaimer when cookie is Invalid', function() {
spyOn(BasketPage, 'isValidLocalCurrencyCookie').and.returnValue(false);
BasketPage.addPriceDisclaimer();
expect(0).toEqual($('.price-disclaimer').length);
});
......@@ -753,6 +754,43 @@ define([
.toEqual($('.price-disclaimer').text());
});
});
describe('isValidLocalCurrencyCookie', function() {
var COOKIE_VALUES = {countryCode: 'FOO', rate: 2, code: 'BAR', symbol: 'BAZ'};
it('should be invalid if undefined', function() {
expect(BasketPage.isValidLocalCurrencyCookie(undefined)).toBe(false);
});
it('should be invalid if no country code exists', function() {
var cookie = _.clone(COOKIE_VALUES);
delete cookie.countryCode;
expect(BasketPage.isValidLocalCurrencyCookie(cookie)).toBe(false);
});
it('should be invalid if no symbol exists', function() {
var cookie = _.clone(COOKIE_VALUES);
delete cookie.symbol;
expect(BasketPage.isValidLocalCurrencyCookie(cookie)).toBe(false);
});
it('should be invalid if no rate exists', function() {
var cookie = _.clone(COOKIE_VALUES);
delete cookie.rate;
expect(BasketPage.isValidLocalCurrencyCookie(cookie)).toBe(false);
});
it('should be invalid if no code exists', function() {
var cookie = _.clone(COOKIE_VALUES);
delete cookie.code;
expect(BasketPage.isValidLocalCurrencyCookie(cookie)).toBe(false);
});
it('should be valid if cookie exists and all necessary fields exist', function() {
var cookie = _.clone(COOKIE_VALUES);
expect(BasketPage.isValidLocalCurrencyCookie(cookie)).toBe(true);
});
});
});
}
);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment