Skip to content

Commit

Permalink
make sure the content type is correct, fixes #13
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco Otte-Witte committed Oct 11, 2013
1 parent a7f4724 commit c0fbc50
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ Ember.SimpleAuth.LoginControllerMixin = Ember.Mixin.create({
var data = this.getProperties('identification', 'password');
if (!Ember.isEmpty(data.identification) && !Ember.isEmpty(data.password)) {
var postData = JSON.stringify(self.serializeCredentials(data.identification, data.password));
Ember.$.post(Ember.SimpleAuth.serverSessionRoute, postData, null, 'json').then(function(response) {
Ember.$.ajax(Ember.SimpleAuth.serverSessionRoute, {
type: 'POST',
data: postData,
contentType: 'application/json'
}).then(function(response) {
self.get('session').setup(response);
var attemptedTransition = self.get('session.attemptedTransition');
if (attemptedTransition) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ var testController = Ember.Controller.extend(Ember.SimpleAuth.LoginControllerMix
}
}).create();

var postRequestUrl;
var postRequestData;
var postMock = function(url, data) {
postRequestUrl = url;
postRequestData = data;
var ajaxRequestUrl;
var ajaxRequestOptions;
var ajaxMock = function(url, options) {
ajaxRequestUrl = url;
ajaxRequestOptions = options;
return {
then: function(success, fail) {
success({ session: { authToken: 'authToken' } });
Expand All @@ -25,30 +25,33 @@ var postMock = function(url, data) {
};

module('Ember.SimpleAuth.LoginControllerMixin', {
originalPost: Ember.$.post,
originalAjax: Ember.$.ajax,
setup: function() {
Ember.SimpleAuth.serverSessionRoute = '/session/route';
testController.setProperties({ identification: 'identification', password: 'password' });
testController.session.destroy();
loginFailedError = undefined;
Ember.$.post = postMock;
loginFailedError = undefined;
Ember.$.ajax = ajaxMock;
ajaxRequestUrl = undefined;
ajaxRequestOptions = undefined;
},
teardown: function() {
Ember.$.post = this.originalPost;
Ember.$.ajax = this.originalAjax;
}
});

test('sends a POST request to the correct route', function() {
testController.send('login');

equal(postRequestUrl, '/session/route', 'Ember.SimpleAuth.LoginControllerMixin sends a request to the correct route on submit.');
equal(postRequestData, '{"session":{"identification":"identification","password":"password"}}', 'Ember.SimpleAuth.LoginControllerMixin sends a request with the correct data on submit.');
equal(ajaxRequestUrl, '/session/route', 'Ember.SimpleAuth.LoginControllerMixin sends a request to the correct route on submit.');
equal(ajaxRequestOptions.data, '{"session":{"identification":"identification","password":"password"}}', 'Ember.SimpleAuth.LoginControllerMixin sends a request with the correct data on submit.');
equal(ajaxRequestOptions.contentType, 'application/json', 'Ember.SimpleAuth.LoginControllerMixin sends a request with the correct content type on submit.');

postRequestUrl = undefined;
ajaxRequestUrl = undefined;
testController.setProperties({ identification: '', password: 'password' });
testController.send('login');

equal(postRequestUrl, undefined, 'Ember.SimpleAuth.LoginControllerMixin does not send a request on submit when identification or password are empty.');
equal(ajaxRequestUrl, undefined, 'Ember.SimpleAuth.LoginControllerMixin does not send a request on submit when identification or password are empty.');
});

test('serializes the credentials correctly when a custom serialization method is provided', function() {
Expand All @@ -59,7 +62,7 @@ test('serializes the credentials correctly when a custom serialization method is
});
testController.send('login');

equal(postRequestData, '{"custom":{"credentials":"identification.password"}}', 'Ember.SimpleAuth.LoginControllerMixin serializes the credentials correctly when a custom serialization method is provided.');
equal(ajaxRequestOptions.data, '{"custom":{"credentials":"identification.password"}}', 'Ember.SimpleAuth.LoginControllerMixin serializes the credentials correctly when a custom serialization method is provided.');
});

test('updates the current session with the server response', function() {
Expand Down

0 comments on commit c0fbc50

Please sign in to comment.