Skip to content

Commit

Permalink
Merge pull request #193 from ParsePlatform/fosco.logout
Browse files Browse the repository at this point in the history
Updated user tests, added /logout
  • Loading branch information
gfosco committed Feb 3, 2016
2 parents 3d3a97d + 373f4f0 commit c290070
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 45 deletions.
82 changes: 39 additions & 43 deletions spec/ParseUser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ describe('Parse.User testing', () => {
sessionToken = newUser.getSessionToken();
ok(sessionToken);

Parse.User.logOut();
return Parse.User.logOut();
}).then(() => {
ok(!Parse.User.current());

return Parse.User.become(sessionToken);
Expand All @@ -91,7 +92,8 @@ describe('Parse.User testing', () => {
equal(newUser.get("username"), "Jason");
equal(newUser.get("code"), "red");

Parse.User.logOut();
return Parse.User.logOut();
}).then(() => {
ok(!Parse.User.current());

return Parse.User.become("somegarbage");
Expand Down Expand Up @@ -236,22 +238,20 @@ describe('Parse.User testing', () => {
user.set("password", "asdf");
user.set("email", "asdf@example.com");
user.set("username", "zxcv");
user.signUp(null, {
success: function() {
var currentUser = Parse.User.current();
equal(user.id, currentUser.id);
ok(user.getSessionToken());

var currentUserAgain = Parse.User.current();
// should be the same object
equal(currentUser, currentUserAgain);
user.signUp().then(() => {
var currentUser = Parse.User.current();
equal(user.id, currentUser.id);
ok(user.getSessionToken());

// test logging out the current user
Parse.User.logOut();
var currentUserAgain = Parse.User.current();
// should be the same object
equal(currentUser, currentUserAgain);

equal(Parse.User.current(), null);
done();
}
// test logging out the current user
return Parse.User.logOut();
}).then(() => {
equal(Parse.User.current(), null);
done();
});
});

Expand Down Expand Up @@ -578,28 +578,24 @@ describe('Parse.User testing', () => {


it("user loaded from localStorage from login", (done) => {
var id;
Parse.User.signUp("alice", "password").then((alice) => {
id = alice.id;
return Parse.User.logOut();
}).then(() => {
return Parse.User.logIn("alice", "password");
}).then((user) => {
// Force the current user to read from disk
delete Parse.User._currentUser;
delete Parse.User._currentUserMatchesDisk;

Parse.User.signUp("alice", "password", null, {
success: function(alice) {
var id = alice.id;
Parse.User.logOut();

Parse.User.logIn("alice", "password", {
success: function(user) {
// Force the current user to read from disk
delete Parse.User._currentUser;
delete Parse.User._currentUserMatchesDisk;

var userFromDisk = Parse.User.current();
equal(userFromDisk.get("password"), undefined,
"password should not be in attributes");
equal(userFromDisk.id, id, "id should be set");
ok(userFromDisk.getSessionToken(),
"currentUser should have a sessionToken");
done();
}
});
}
var userFromDisk = Parse.User.current();
equal(userFromDisk.get("password"), undefined,
"password should not be in attributes");
equal(userFromDisk.id, id, "id should be set");
ok(userFromDisk.getSessionToken(),
"currentUser should have a sessionToken");
done();
});
});

Expand All @@ -609,8 +605,8 @@ describe('Parse.User testing', () => {

Parse.User.signUp("alice", "password", null).then(function(alice) {
id = alice.id;
Parse.User.logOut();

return Parse.User.logOut();
}).then(() => {
return Parse.User.logIn("alice", "password");
}).then(function() {
// Simulate browser refresh by force-reloading user from localStorage
Expand Down Expand Up @@ -1300,17 +1296,17 @@ describe('Parse.User testing', () => {
return Parse.User.signUp("finn", "human", { foo: "bar" });

}).then(function() {
Parse.User.logOut();

return Parse.User.logOut();
}).then(() => {
var user = new Parse.User();
user.set("username", "jake");
user.set("password", "dog");
user.set("foo", "baz");
return user.signUp();

}).then(function() {
Parse.User.logOut();

return Parse.User.logOut();
}).then(() => {
var query = new Parse.Query(Parse.User);
return query.find();

Expand Down
3 changes: 1 addition & 2 deletions spec/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ beforeEach(function(done) {
});

afterEach(function(done) {
Parse.User.logOut();
Parse.Promise.as().then(() => {
Parse.User.logOut().then(() => {
return clearData();
}).then(() => {
done();
Expand Down
17 changes: 17 additions & 0 deletions users.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,22 @@ function handleDelete(req) {
});
}

function handleLogOut(req) {
var success = {response: {}};
if (req.info && req.info.sessionToken) {
rest.find(req.config, Auth.master(req.config), '_Session',
{_session_token: req.info.sessionToken}
).then((records) => {
if (records.results && records.results.length) {
rest.del(req.config, Auth.master(req.config), '_Session',
records.results[0].id
);
}
});
}
return Promise.resolve(success);
}

function handleUpdate(req) {
return rest.update(req.config, req.auth, '_User',
req.params.objectId, req.body)
Expand All @@ -176,6 +192,7 @@ function notImplementedYet(req) {

router.route('POST', '/users', handleCreate);
router.route('GET', '/login', handleLogIn);
router.route('POST', '/logout', handleLogOut);
router.route('GET', '/users/me', handleMe);
router.route('GET', '/users/:objectId', handleGet);
router.route('PUT', '/users/:objectId', handleUpdate);
Expand Down

0 comments on commit c290070

Please sign in to comment.