Skip to content

Commit 36e44b1

Browse files
committed
updated spec
1 parent ff6ddd2 commit 36e44b1

File tree

5 files changed

+150
-48
lines changed

5 files changed

+150
-48
lines changed

spec/AuthenticationAdapters.spec.js

Lines changed: 121 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1788,12 +1788,12 @@ describe('Auth Adapter features', () => {
17881788
},
17891789
};
17901790

1791-
/* const modernAdapter = {
1791+
const modernAdapter = {
17921792
validateAppId: () => Promise.resolve(),
17931793
validateSetUp: () => Promise.resolve(),
17941794
validateUpdate: () => Promise.resolve(),
17951795
validateLogin: () => Promise.resolve(),
1796-
}; */
1796+
};
17971797

17981798
const headers = {
17991799
'Content-Type': 'application/json',
@@ -1846,9 +1846,105 @@ describe('Auth Adapter features', () => {
18461846
expect(secondCall[3].id).toEqual(user.id);
18471847
});
18481848

1849-
xit('should trigger correctly validateSetUp');
1850-
xit('should trigger correctly validateLogin');
1851-
xit('should trigger correctly validateUpdate');
1849+
it('should trigger correctly validateSetUp', async () => {
1850+
spyOn(modernAdapter, 'validateSetUp').and.resolveTo({});
1851+
spyOn(modernAdapter, 'validateUpdate').and.resolveTo({});
1852+
spyOn(modernAdapter, 'validateLogin').and.resolveTo({});
1853+
1854+
await reconfigureServer({ auth: { modernAdapter } });
1855+
const user = new Parse.User();
1856+
1857+
await user.save({ authData: { modernAdapter: { id: 'modernAdapter' } } });
1858+
1859+
expect(modernAdapter.validateUpdate).toHaveBeenCalledTimes(0);
1860+
expect(modernAdapter.validateLogin).toHaveBeenCalledTimes(0);
1861+
expect(modernAdapter.validateSetUp).toHaveBeenCalledTimes(1);
1862+
const call = modernAdapter.validateSetUp.calls.argsFor(0);
1863+
expect(call[0]).toEqual({ id: 'modernAdapter' });
1864+
expect(call[1]).toEqual(modernAdapter);
1865+
expect(call[2].config).toBeDefined();
1866+
expect(call[2].auth).toBeDefined();
1867+
expect(call[2].config.headers).toBeDefined();
1868+
expect(call[3]).toBeUndefined();
1869+
expect(user.getSessionToken()).toBeDefined();
1870+
});
1871+
it('should trigger correctly validateLogin', async () => {
1872+
spyOn(modernAdapter, 'validateSetUp').and.resolveTo({});
1873+
spyOn(modernAdapter, 'validateUpdate').and.resolveTo({});
1874+
spyOn(modernAdapter, 'validateLogin').and.resolveTo({});
1875+
1876+
await reconfigureServer({ auth: { modernAdapter } });
1877+
const user = new Parse.User();
1878+
1879+
// Signup
1880+
await user.save({ authData: { modernAdapter: { id: 'modernAdapter' } } });
1881+
1882+
expect(modernAdapter.validateSetUp).toHaveBeenCalledTimes(1);
1883+
// Login
1884+
const user2 = new Parse.User();
1885+
await user2.save({ authData: { modernAdapter: { id: 'modernAdapter' } } });
1886+
1887+
expect(modernAdapter.validateUpdate).toHaveBeenCalledTimes(0);
1888+
expect(modernAdapter.validateSetUp).toHaveBeenCalledTimes(1);
1889+
expect(modernAdapter.validateLogin).toHaveBeenCalledTimes(1);
1890+
const call = modernAdapter.validateLogin.calls.argsFor(0);
1891+
expect(call[0]).toEqual({ id: 'modernAdapter' });
1892+
expect(call[1]).toEqual(modernAdapter);
1893+
expect(call[2].config).toBeDefined();
1894+
expect(call[2].auth).toBeDefined();
1895+
expect(call[2].config.headers).toBeDefined();
1896+
expect(call[3] instanceof Parse.User).toBeTruthy();
1897+
expect(call[3].id).toEqual(user2.id);
1898+
expect(call[3].id).toEqual(user.id);
1899+
expect(user2.getSessionToken()).toBeDefined();
1900+
});
1901+
it('should trigger correctly validateUpdate', async () => {
1902+
spyOn(modernAdapter, 'validateSetUp').and.resolveTo({});
1903+
spyOn(modernAdapter, 'validateUpdate').and.resolveTo({});
1904+
spyOn(modernAdapter, 'validateLogin').and.resolveTo({});
1905+
1906+
await reconfigureServer({ auth: { modernAdapter } });
1907+
const user = new Parse.User();
1908+
1909+
// Signup
1910+
await user.save({ authData: { modernAdapter: { id: 'modernAdapter' } } });
1911+
expect(modernAdapter.validateSetUp).toHaveBeenCalledTimes(1);
1912+
1913+
// Save same data
1914+
await user.save(
1915+
{ authData: { modernAdapter: { id: 'modernAdapter' } } },
1916+
{ sessionToken: user.getSessionToken() }
1917+
);
1918+
1919+
// Save same data with master key
1920+
await user.save(
1921+
{ authData: { modernAdapter: { id: 'modernAdapter' } } },
1922+
{ useMasterKey: true }
1923+
);
1924+
1925+
expect(modernAdapter.validateUpdate).toHaveBeenCalledTimes(0);
1926+
expect(modernAdapter.validateSetUp).toHaveBeenCalledTimes(1);
1927+
expect(modernAdapter.validateLogin).toHaveBeenCalledTimes(0);
1928+
1929+
// Change authData
1930+
await user.save(
1931+
{ authData: { modernAdapter: { id: 'modernAdapter2' } } },
1932+
{ sessionToken: user.getSessionToken() }
1933+
);
1934+
1935+
expect(modernAdapter.validateUpdate).toHaveBeenCalledTimes(1);
1936+
expect(modernAdapter.validateSetUp).toHaveBeenCalledTimes(1);
1937+
expect(modernAdapter.validateLogin).toHaveBeenCalledTimes(0);
1938+
const call = modernAdapter.validateUpdate.calls.argsFor(0);
1939+
expect(call[0]).toEqual({ id: 'modernAdapter2' });
1940+
expect(call[1]).toEqual(modernAdapter);
1941+
expect(call[2].config).toBeDefined();
1942+
expect(call[2].auth).toBeDefined();
1943+
expect(call[2].config.headers).toBeDefined();
1944+
expect(call[3] instanceof Parse.User).toBeTruthy();
1945+
expect(call[3].id).toEqual(user.id);
1946+
expect(user.getSessionToken()).toBeDefined();
1947+
});
18521948
xit('should throw if no triggers found');
18531949
it('should not update authData if provider return doNotSave', async () => {
18541950
spyOn(doNotSaveAdapter, 'validateAuthData').and.resolveTo({ doNotSave: true });
@@ -1988,14 +2084,32 @@ describe('Auth Adapter features', () => {
19882084
});
19892085

19902086
const user2 = new Parse.User();
1991-
await user2.save({
2087+
user2.id = user.id;
2088+
await user2.save(
2089+
{
2090+
authData: {
2091+
baseAdapter: { id: 'baseAdapter' },
2092+
alwaysValidateAdapter: { test: true },
2093+
},
2094+
},
2095+
{ sessionToken: user.getSessionToken() }
2096+
);
2097+
2098+
expect(user2.get('authDataResponse')).toEqual({ alwaysValidateAdapter: { someData2: true } });
2099+
2100+
const user3 = new Parse.User();
2101+
await user3.save({
19922102
authData: {
19932103
baseAdapter: { id: 'baseAdapter' },
19942104
alwaysValidateAdapter: { test: true },
19952105
},
19962106
});
19972107

1998-
expect(user2.get('authDataResponse')).toEqual({ alwaysValidateAdapter: { someData2: true } });
2108+
// On logIn all authData are revalidated
2109+
expect(user3.get('authDataResponse')).toEqual({
2110+
baseAdapter: { someData: true },
2111+
alwaysValidateAdapter: { someData2: true },
2112+
});
19992113

20002114
const userViaMasterKey = new Parse.User();
20012115
userViaMasterKey.id = user2.id;

spec/ParseUser.spec.js

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1789,7 +1789,7 @@ describe('Parse.User testing', () => {
17891789
});
17901790
});
17911791

1792-
it('should allow login with old authData token', done => {
1792+
it('should not allow login with old authData token', async () => {
17931793
const provider = {
17941794
authData: {
17951795
id: '12345',
@@ -1810,22 +1810,17 @@ describe('Parse.User testing', () => {
18101810
};
18111811
defaultConfiguration.auth.shortLivedAuth.setValidAccessToken('token');
18121812
Parse.User._registerAuthenticationProvider(provider);
1813-
Parse.User._logInWith('shortLivedAuth', {})
1814-
.then(() => {
1815-
// Simulate a remotely expired token (like a short lived one)
1816-
// In this case, we want success as it was valid once.
1817-
// If the client needs an updated one, do lock the user out
1818-
defaultConfiguration.auth.shortLivedAuth.setValidAccessToken('otherToken');
1819-
return Parse.User._logInWith('shortLivedAuth', {});
1820-
})
1821-
.then(
1822-
() => {
1823-
done();
1824-
},
1825-
err => {
1826-
done.fail(err);
1827-
}
1828-
);
1813+
await Parse.User._logInWith('shortLivedAuth', {});
1814+
// Simulate a remotely expired token (like a short lived one)
1815+
// In this case, we want success as it was valid once.
1816+
// If the client needs an updated one, do lock the user out
1817+
defaultConfiguration.auth.shortLivedAuth.setValidAccessToken('otherToken');
1818+
try {
1819+
await Parse.User._logInWith('shortLivedAuth', {});
1820+
fail('should not authenticate');
1821+
} catch (e) {
1822+
expect(e).toBeDefined();
1823+
}
18291824
});
18301825

18311826
it('should allow PUT request with stale auth Data', done => {

src/Adapters/Auth/index.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ const providers = {
6464

6565
function authDataValidator(provider, adapter, appIds, options) {
6666
return async function (authData, req, user) {
67-
if (appIds) {
67+
if (appIds && typeof adapter.validateAppId === 'function') {
6868
await adapter.validateAppId(appIds, authData, options, req, user);
6969
}
7070
if (typeof adapter.validateAuthData === 'function') {
@@ -130,22 +130,20 @@ function loadAuthAdapter(provider, authOptions) {
130130
if (providerOptions) {
131131
const optionalAdapter = loadAdapter(providerOptions, undefined, providerOptions);
132132
if (optionalAdapter) {
133-
['validateAuthData', 'validateAppId'].forEach(key => {
133+
[
134+
'validateAuthData',
135+
'validateAppId',
136+
'validateSetUp',
137+
'validateLogin',
138+
'validateUpdate',
139+
].forEach(key => {
134140
if (optionalAdapter[key]) {
135141
adapter[key] = optionalAdapter[key];
136142
}
137143
});
138144
}
139145
}
140146

141-
// TODO: create a new module from validateAdapter() in
142-
// src/Controllers/AdaptableController.js so we can use it here for adapter
143-
// validation based on the src/Adapters/Auth/AuthAdapter.js expected class
144-
// signature.
145-
if (!adapter.validateAuthData || !adapter.validateAppId) {
146-
return;
147-
}
148-
149147
return { adapter, appIds, providerOptions };
150148
}
151149

src/Auth.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ const checkIfUserHasProvidedConfiguredProvidersForLogin = (
425425
};
426426

427427
// Validate each authData step by step and return the provider responses
428-
const handleAuthDataValidation = (authData, req, foundUser) => {
428+
const handleAuthDataValidation = async (authData, req, foundUser) => {
429429
let user;
430430
if (foundUser) {
431431
user = Parse.User.fromJSON({ className: '_User', ...foundUser });
@@ -440,7 +440,7 @@ const handleAuthDataValidation = (authData, req, foundUser) => {
440440
) {
441441
user = new Parse.User();
442442
user.id = req.auth.isMaster ? req.getUserId() : req.auth.user.id;
443-
user.fetch({ useMasterKey: true });
443+
await user.fetch({ useMasterKey: true });
444444
}
445445

446446
// Perform validation as step by step pipeline

src/RestWrite.js

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -527,20 +527,15 @@ RestWrite.prototype.handleAuthData = async function (authData) {
527527
);
528528
}
529529

530-
// We have authData that is updated on login
531-
// that can happen when token are refreshed,
532-
// We should update the token and let the user in
533-
// We should only check the mutated keys
534-
const { authData: validatedAuthData, authDataResponse } = await Auth.handleAuthDataValidation(
535-
mutatedAuthData,
530+
// Force to validate all provided authData on login
531+
// on update only validate mutated ones
532+
const res = await Auth.handleAuthDataValidation(
533+
isLogin ? authData : mutatedAuthData,
536534
this,
537535
userResult
538536
);
539-
540-
// Replace current authData by the new validated one
541-
this.data.authData = validatedAuthData;
542-
543-
this.authDataResponse = authDataResponse;
537+
this.data.authData = res.authData;
538+
this.authDataResponse = res.authDataResponse;
544539

545540
// IF we are in login we'll skip the database operation / beforeSave / afterSave etc...
546541
// we need to set it up there.
@@ -558,7 +553,7 @@ RestWrite.prototype.handleAuthData = async function (authData) {
558553
await this.config.database.update(
559554
this.className,
560555
{ objectId: this.data.objectId },
561-
{ authData: validatedAuthData },
556+
{ authData: this.data.authData },
562557
{}
563558
);
564559
}

0 commit comments

Comments
 (0)