Skip to content

Commit 96d8409

Browse files
committed
Use REST implementation to avoid side effects for username/email duplicates
1 parent ee34a79 commit 96d8409

File tree

1 file changed

+59
-65
lines changed

1 file changed

+59
-65
lines changed

spec/ParseAPI.spec.js

Lines changed: 59 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ const SchemaController = require('../lib/Controllers/SchemaController');
1010
const TestUtils = require('../lib/TestUtils');
1111

1212
const userSchema = SchemaController.convertSchemaToAdapterSchema({ className: '_User', fields: Object.assign({}, SchemaController.defaultColumns._Default, SchemaController.defaultColumns._User) });
13+
const headers = {
14+
'Content-Type': 'application/json',
15+
'X-Parse-Application-Id': 'test',
16+
'X-Parse-REST-API-Key': 'rest',
17+
'X-Parse-Installation-Id': 'yolo'
18+
}
1319

1420
describe_only_db('mongo')('miscellaneous', () => {
1521
it('test rest_create_app', function(done) {
@@ -67,91 +73,79 @@ describe('miscellaneous', function() {
6773
}, done.fail);
6874
});
6975

70-
it('fail to create a duplicate username', async done => {
71-
try {
72-
await Parse.User.logOut();
73-
} catch(e) { /* ignore */ }
74-
let numCreated = 0;
76+
it('fail to create a duplicate username', async () => {
7577
let numFailed = 0;
76-
const p1 = createTestUser();
77-
p1.then(() => {
78+
let numCreated = 0;
79+
const p1 = rp.post(Parse.serverURL + '/users', {
80+
json: {
81+
password: 'asdf',
82+
username: 'u1',
83+
84+
},
85+
headers
86+
}).then(() => {
7887
numCreated++;
7988
expect(numCreated).toEqual(1);
80-
})
81-
.catch(error => {
82-
numFailed++;
83-
expect(numFailed).toEqual(1);
84-
expect(error.code).toEqual(Parse.Error.USERNAME_TAKEN);
85-
});
86-
const p2 = createTestUser();
87-
p2.then(() => {
89+
}, ({ error }) => {
90+
numFailed++;
91+
expect(error.code).toEqual(Parse.Error.USERNAME_TAKEN);
92+
});
93+
94+
const p2 = rp.post(Parse.serverURL + '/users', {
95+
json: {
96+
password: 'asdf',
97+
username: 'u1',
98+
99+
},
100+
headers
101+
}).then(() => {
88102
numCreated++;
89-
expect(numCreated).toEqual(1);
90-
})
91-
.catch(error => {
92-
numFailed++;
93-
expect(numFailed).toEqual(1);
94-
expect(error.code).toEqual(Parse.Error.USERNAME_TAKEN);
95-
});
96-
Promise.all([p1, p2])
97-
.then(() => {
98-
fail('one of the users should not have been created');
99-
done();
100-
})
101-
.catch(() => {})
102-
.finally(async () => {
103-
try {
104-
await Parse.User.logOut();
105-
} catch(e) { /* ignore */ }
106-
done();
107-
});
103+
}, ({ error }) => {
104+
numFailed++;
105+
expect(error.code).toEqual(Parse.Error.USERNAME_TAKEN);
106+
});
107+
108+
await Promise.all([p1, p2])
109+
expect(numFailed).toEqual(1);
110+
expect(numCreated).toBe(1);
108111
});
109112

110-
it('ensure that email is uniquely indexed', async done => {
111-
try {
112-
await Parse.User.logOut();
113-
} catch(e) { /* ignore */ }
113+
it('ensure that email is uniquely indexed', async () => {
114114
let numFailed = 0;
115115
let numCreated = 0;
116-
const user1 = new Parse.User();
117-
user1.setPassword('asdf');
118-
user1.setUsername('u1');
119-
user1.setEmail('[email protected]');
120-
const p1 = user1.signUp();
121-
p1.then(() => {
116+
const p1 = rp.post(Parse.serverURL + '/users', {
117+
json: {
118+
password: 'asdf',
119+
username: 'u1',
120+
121+
},
122+
headers
123+
}).then(() => {
122124
numCreated++;
123125
expect(numCreated).toEqual(1);
124-
}, error => {
126+
}, ({ error }) => {
125127
numFailed++;
126-
expect(numFailed).toEqual(1);
127128
expect(error.code).toEqual(Parse.Error.EMAIL_TAKEN);
128129
});
129130

130-
const user2 = new Parse.User();
131-
user2.setPassword('asdf');
132-
user2.setUsername('u2');
133-
user2.setEmail('[email protected]');
134-
const p2 = user2.signUp();
135-
p2.then(() => {
131+
const p2 = rp.post(Parse.serverURL + '/users', {
132+
json: {
133+
password: 'asdf',
134+
username: 'u2',
135+
136+
},
137+
headers
138+
}).then(() => {
136139
numCreated++;
137140
expect(numCreated).toEqual(1);
138-
}, error => {
141+
}, ({ error }) => {
139142
numFailed++;
140-
expect(numFailed).toEqual(1);
141143
expect(error.code).toEqual(Parse.Error.EMAIL_TAKEN);
142144
});
143145

144-
Promise.all([p1, p2])
145-
.then(() => {
146-
fail('one of the users should not have been created');
147-
})
148-
.catch(() => {})
149-
.finally(async () => {
150-
try {
151-
await Parse.User.logOut();
152-
} catch(e) { /* ignore */ }
153-
done();
154-
});
146+
await Promise.all([p1, p2])
147+
expect(numFailed).toEqual(1);
148+
expect(numCreated).toBe(1);
155149
});
156150

157151
it('ensure that if people already have duplicate users, they can still sign up new users', async done => {

0 commit comments

Comments
 (0)