Skip to content
Snippets Groups Projects
Unverified Commit efb4cd94 authored by irfanuddinahmad's avatar irfanuddinahmad Committed by GitHub
Browse files

Merge pull request #23532 from...

Merge pull request #23532 from edx/iahmad/ENT-2725-Implicit-Enterprise-Selection-When-Enrolling-With-Url

ENT-2725 added support for enrolment urls in multiple enterprise case
parents baabbc5f d105aac8
Branches
Tags
No related merge requests found
......@@ -9,7 +9,8 @@ define([
describe('MultipleEnterpriseInterface', function() {
var LEARNER_URL = '/enterprise/api/v1/enterprise-learner/?username=test-learner',
NEXT_URL = '/dashboard',
REDIRECT_URL = '/enterprise/select/active/?success_url=%2Fdashboard';
REDIRECT_URL = '/enterprise/select/active/?success_url=%2Fdashboard',
ENTERPRISE_ACTIVATION_URL = '/enterprise/select/active';
beforeEach(function() {
// Mock the redirect call
......@@ -39,6 +40,75 @@ define([
expect(MultipleEnterpriseInterface.redirect).toHaveBeenCalledWith(REDIRECT_URL);
});
it('checks bypass of enterprise selection page in case of enterprise in URL', function() {
// Spy on Ajax requests
var requests = AjaxHelpers.requests(this);
spyOn(MultipleEnterpriseInterface, 'getEnterpriseFromUrl').and.returnValue('SomeEnterprise');
spyOn(MultipleEnterpriseInterface, 'checkEnterpriseExists').and.returnValue(true);
// Attempt to fetch a learner
MultipleEnterpriseInterface.check(NEXT_URL);
// Expect that the correct request was made to the server
AjaxHelpers.expectRequest(
requests,
'GET',
LEARNER_URL,
null
);
// Simulate a successful response from the server
AjaxHelpers.respondWithJson(requests, {count: 2});
AjaxHelpers.expectRequest(
requests,
'POST',
ENTERPRISE_ACTIVATION_URL,
$.param({
enterprise: 'SomeEnterprise'
})
);
AjaxHelpers.respondWithNoContent(requests);
// Verify that the user was redirected correctly
expect(MultipleEnterpriseInterface.redirect).toHaveBeenCalledWith(NEXT_URL);
});
it('checks enterprise selection page redirect in case of enterprise activation failure', function() {
// Spy on Ajax requests
var requests = AjaxHelpers.requests(this);
spyOn(MultipleEnterpriseInterface, 'getEnterpriseFromUrl').and.returnValue('SomeEnterprise');
spyOn(MultipleEnterpriseInterface, 'checkEnterpriseExists').and.returnValue(true);
// Attempt to fetch a learner
MultipleEnterpriseInterface.check(NEXT_URL);
// Expect that the correct request was made to the server
AjaxHelpers.expectRequest(
requests,
'GET',
LEARNER_URL,
null
);
// Simulate a successful response from the server
AjaxHelpers.respondWithJson(requests, {count: 2});
AjaxHelpers.expectRequest(
requests,
'POST',
ENTERPRISE_ACTIVATION_URL,
$.param({
enterprise: 'SomeEnterprise'
})
);
// Simulate an error response from the server
AjaxHelpers.respondWithError(requests);
// Verify that the user was redirected correctly
expect(MultipleEnterpriseInterface.redirect).toHaveBeenCalledWith(REDIRECT_URL);
});
it('gets learner information and checks that enterprise selection page is bypassed', function() {
// Spy on Ajax requests
var requests = AjaxHelpers.requests(this);
......
......@@ -5,7 +5,8 @@
urls: {
learners: '/enterprise/api/v1/enterprise-learner/',
multipleEnterpriseUrl: '/enterprise/select/active/?success_url='
multipleEnterpriseUrl: '/enterprise/select/active/?success_url=',
enterpriseActivationUrl: '/enterprise/select/active'
},
headers: {
......@@ -15,12 +16,17 @@
/**
* Fetch the learner data, then redirect the user to a enterprise selection page if multiple
* enterprises were found.
* @param {string} nextUrl The URL to redirect to after multiple enterprise selection.
* @param {string} nextUrl The URL to redirect to after multiple enterprise selection or incase
* the selection page is bypassed e.g. when dealing with direct enrolment urls.
*/
check: function(nextUrl) {
var redirectUrl = this.urls.multipleEnterpriseUrl + encodeURIComponent(nextUrl);
var view = this;
var selectionPageUrl = this.urls.multipleEnterpriseUrl + encodeURIComponent(nextUrl);
var username = Utils.userFromEdxUserCookie().username;
var next = nextUrl || '/';
var enterpriseInUrl = this.getEnterpriseFromUrl(nextUrl);
var userInEnterprise = false;
var userWithMultipleEnterprises = false;
$.ajax({
url: this.urls.learners + '?username=' + username,
type: 'GET',
......@@ -28,22 +34,53 @@
headers: this.headers,
context: this
}).fail(function() {
this.redirect(next);
view.redirect(next);
}).done(function(response) {
if (response.count > 1 && redirectUrl) {
this.redirect(redirectUrl);
userWithMultipleEnterprises = (response.count > 1);
if (userWithMultipleEnterprises) {
if (enterpriseInUrl) {
userInEnterprise = view.checkEnterpriseExists(response, enterpriseInUrl);
if (userInEnterprise) {
view.activate(enterpriseInUrl).fail(function() {
view.redirect(selectionPageUrl);
}).done(function() {
view.redirect(next);
});
}
}
view.redirect(selectionPageUrl);
} else {
this.redirect(next);
view.redirect(next);
}
});
},
/**
* Redirect to a URL.
* @param {string} url The URL to redirect to.
*/
redirect: function(url) {
window.location.href = url;
},
activate: function(enterprise) {
return $.ajax({
url: this.urls.enterpriseActivationUrl,
method: 'POST',
headers: {'X-CSRFToken': $.cookie('csrftoken')},
data: {enterprise: enterprise}
});
},
getEnterpriseFromUrl: function(url) {
var regex;
regex = RegExp('/enterprise/.*/course/.*/enroll');
if (typeof url !== 'string' || !regex.test(url)) {
return void(0);
}
return url.split('/')[2];
},
checkEnterpriseExists: function(response, enterprise) {
return response.results.some(function(item) {
return item.enterprise_customer.uuid === enterprise;
});
}
};
......
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