Skip to content

Commit e2bcc87

Browse files
committed
Merge pull request #1081 from ParsePlatform/flovilmart.deleteAuthData
Ensures _auth_data_[provider] gets deleted from document
2 parents 7194fb1 + 5ff186f commit e2bcc87

File tree

2 files changed

+48
-31
lines changed

2 files changed

+48
-31
lines changed

spec/OAuth.spec.js

+38-27
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var OAuth = require("../src/authDataManager/OAuth1Client");
22
var request = require('request');
3+
var Config = require("../src/Config");
34

45
describe('OAuth', function() {
56

@@ -28,7 +29,7 @@ describe('OAuth', function() {
2829

2930
it("Should properly generate request signature", (done) => {
3031
var request = {
31-
host: "dummy.com",
32+
host: "dummy.com",
3233
path: "path"
3334
};
3435

@@ -48,7 +49,7 @@ describe('OAuth', function() {
4849

4950
it("Should properly build request", (done) => {
5051
var options = {
51-
host: "dummy.com",
52+
host: "dummy.com",
5253
consumer_key: "hello",
5354
consumer_secret: "world",
5455
auth_token: "token",
@@ -86,7 +87,7 @@ describe('OAuth', function() {
8687

8788
it("Should fail a GET request", (done) => {
8889
var options = {
89-
host: "api.twitter.com",
90+
host: "api.twitter.com",
9091
consumer_key: "XXXXXXXXXXXXXXXXXXXXXXXXX",
9192
consumer_secret: "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
9293
};
@@ -100,7 +101,7 @@ describe('OAuth', function() {
100101

101102
it("Should fail a POST request", (done) => {
102103
var options = {
103-
host: "api.twitter.com",
104+
host: "api.twitter.com",
104105
consumer_key: "XXXXXXXXXXXXXXXXXXXXXXXXX",
105106
consumer_secret: "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
106107
};
@@ -117,7 +118,7 @@ describe('OAuth', function() {
117118

118119
it("Should fail a request", (done) => {
119120
var options = {
120-
host: "localhost",
121+
host: "localhost",
121122
consumer_key: "XXXXXXXXXXXXXXXXXXXXXXXXX",
122123
consumer_secret: "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
123124
};
@@ -141,8 +142,8 @@ describe('OAuth', function() {
141142
var provider = require("../src/authDataManager/"+providerName);
142143
jequal(typeof provider.validateAuthData, "function");
143144
jequal(typeof provider.validateAppId, "function");
144-
jequal(provider.validateAuthData({}, {}).constructor, Promise.prototype.constructor);
145-
jequal(provider.validateAppId("app", "key", {}).constructor, Promise.prototype.constructor);
145+
jequal(provider.validateAuthData({}, {}).constructor, Promise.prototype.constructor);
146+
jequal(provider.validateAppId("app", "key", {}).constructor, Promise.prototype.constructor);
146147
done();
147148
});
148149
});
@@ -204,12 +205,12 @@ describe('OAuth', function() {
204205
}
205206
};
206207
var headers = {'X-Parse-Application-Id': 'test',
207-
'X-Parse-REST-API-Key': 'rest',
208+
'X-Parse-REST-API-Key': 'rest',
208209
'Content-Type': 'application/json' }
209210

210211
var options = {
211212
headers: {'X-Parse-Application-Id': 'test',
212-
'X-Parse-REST-API-Key': 'rest',
213+
'X-Parse-REST-API-Key': 'rest',
213214
'Content-Type': 'application/json' },
214215
url: 'http://localhost:8378/1/users',
215216
body: JSON.stringify(jsonBody)
@@ -248,7 +249,7 @@ describe('OAuth', function() {
248249
done();
249250
});
250251
});
251-
252+
252253
});
253254

254255
it("unlink and link with custom provider", (done) => {
@@ -266,28 +267,38 @@ describe('OAuth', function() {
266267

267268
model._unlinkFrom("myoauth", {
268269
success: function(model) {
270+
269271
ok(!model._isLinked("myoauth"),
270272
"User should not be linked to myoauth");
271273
ok(!provider.synchronizedUserId, "User id should be cleared");
272274
ok(!provider.synchronizedAuthToken, "Auth token should be cleared");
273275
ok(!provider.synchronizedExpiration,
274276
"Expiration should be cleared");
275-
276-
model._linkWith("myoauth", {
277-
success: function(model) {
278-
ok(provider.synchronizedUserId, "User id should have a value");
279-
ok(provider.synchronizedAuthToken,
280-
"Auth token should have a value");
281-
ok(provider.synchronizedExpiration,
282-
"Expiration should have a value");
283-
ok(model._isLinked("myoauth"),
284-
"User should be linked to myoauth");
285-
done();
286-
},
287-
error: function(model, error) {
288-
ok(false, "linking again should succeed");
289-
done();
290-
}
277+
// make sure the auth data is properly deleted
278+
var config = new Config(Parse.applicationId);
279+
config.database.mongoFind('_User', {
280+
_id: model.id
281+
}).then((res) => {
282+
expect(res.length).toBe(1);
283+
expect(res[0]._auth_data_myoauth).toBeUndefined();
284+
expect(res[0]._auth_data_myoauth).not.toBeNull();
285+
286+
model._linkWith("myoauth", {
287+
success: function(model) {
288+
ok(provider.synchronizedUserId, "User id should have a value");
289+
ok(provider.synchronizedAuthToken,
290+
"Auth token should have a value");
291+
ok(provider.synchronizedExpiration,
292+
"Expiration should have a value");
293+
ok(model._isLinked("myoauth"),
294+
"User should be linked to myoauth");
295+
done();
296+
},
297+
error: function(model, error) {
298+
ok(false, "linking again should succeed");
299+
done();
300+
}
301+
});
291302
});
292303
},
293304
error: function(model, error) {
@@ -304,4 +315,4 @@ describe('OAuth', function() {
304315
});
305316

306317

307-
})
318+
})

src/transform.js

+10-4
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export function transformKeyValue(schema, className, restKey, restValue, options
8787
return transformWhere(schema, className, s);
8888
});
8989
return {key: '$and', value: mongoSubqueries};
90-
default:
90+
default:
9191
// Other auth data
9292
var authDataMatch = key.match(/^authData\.([a-zA-Z0-9_]+)\.id$/);
9393
if (authDataMatch) {
@@ -224,7 +224,7 @@ function transformUpdate(schema, className, restUpdate) {
224224
if (className == '_User') {
225225
restUpdate = transformAuthData(restUpdate);
226226
}
227-
227+
228228
var mongoUpdate = {};
229229
var acl = transformACL(restUpdate);
230230
if (acl._rperm || acl._wperm) {
@@ -260,7 +260,14 @@ function transformUpdate(schema, className, restUpdate) {
260260
function transformAuthData(restObject) {
261261
if (restObject.authData) {
262262
Object.keys(restObject.authData).forEach((provider) => {
263-
restObject[`_auth_data_${provider}`] = restObject.authData[provider];
263+
let providerData = restObject.authData[provider];
264+
if (providerData == null) {
265+
restObject[`_auth_data_${provider}`] = {
266+
__op: 'Delete'
267+
}
268+
} else {
269+
restObject[`_auth_data_${provider}`] = providerData;
270+
}
264271
});
265272
delete restObject.authData;
266273
}
@@ -823,4 +830,3 @@ module.exports = {
823830
transformWhere: transformWhere,
824831
untransformObject: untransformObject
825832
};
826-

0 commit comments

Comments
 (0)