Skip to content

Commit 96274f4

Browse files
committed
Add test, fix coverage
1 parent 36e44b1 commit 96274f4

File tree

4 files changed

+69
-63
lines changed

4 files changed

+69
-63
lines changed

spec/AuthenticationAdapters.spec.js

Lines changed: 66 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1754,17 +1754,15 @@ describe('Auth Adapter features', () => {
17541754
validateAppId: () => Promise.resolve(),
17551755
validateAuthData: () => Promise.resolve(),
17561756
};
1757-
const alwaysValidateAdapter = {
1757+
const baseAdapter2 = {
17581758
validateAppId: () => Promise.resolve(),
17591759
validateAuthData: () => Promise.resolve(),
17601760
options: { anOption: true },
1761-
alwaysValidate: true,
17621761
};
17631762

17641763
const doNotSaveAdapter = {
17651764
validateAppId: () => Promise.resolve(),
17661765
validateAuthData: () => Promise.resolve({ doNotSave: true }),
1767-
alwaysValidate: true,
17681766
};
17691767

17701768
const additionalAdapter = {
@@ -1795,15 +1793,19 @@ describe('Auth Adapter features', () => {
17951793
validateLogin: () => Promise.resolve(),
17961794
};
17971795

1796+
const wrongAdapter = {
1797+
validateAppId: () => Promise.resolve(),
1798+
};
1799+
17981800
const headers = {
17991801
'Content-Type': 'application/json',
18001802
'X-Parse-Application-Id': 'test',
18011803
'X-Parse-REST-API-Key': 'rest',
18021804
};
18031805

18041806
it('should pass authData, options, req, user to validateAuthData', async () => {
1805-
spyOn(alwaysValidateAdapter, 'validateAuthData').and.resolveTo({});
1806-
await reconfigureServer({ auth: { alwaysValidateAdapter } });
1807+
spyOn(baseAdapter, 'validateAuthData').and.resolveTo({});
1808+
await reconfigureServer({ auth: { baseAdapter } });
18071809

18081810
const user = new Parse.User();
18091811

@@ -1812,14 +1814,14 @@ describe('Auth Adapter features', () => {
18121814
await user.save({
18131815
username: 'test',
18141816
password: 'password',
1815-
authData: { alwaysValidateAdapter: payload },
1817+
authData: { baseAdapter: payload },
18161818
});
18171819

18181820
expect(user.getSessionToken()).toBeDefined();
18191821

1820-
const firstCall = alwaysValidateAdapter.validateAuthData.calls.argsFor(0);
1822+
const firstCall = baseAdapter.validateAuthData.calls.argsFor(0);
18211823
expect(firstCall[0]).toEqual(payload);
1822-
expect(firstCall[1]).toEqual(alwaysValidateAdapter);
1824+
expect(firstCall[1]).toEqual(baseAdapter);
18231825
expect(firstCall[2].config).toBeDefined();
18241826
expect(firstCall[2].config.headers).toBeDefined();
18251827
expect(firstCall[2].auth).toBeDefined();
@@ -1833,12 +1835,12 @@ describe('Auth Adapter features', () => {
18331835
body: JSON.stringify({
18341836
username: 'test',
18351837
password: 'password',
1836-
authData: { alwaysValidateAdapter: payload },
1838+
authData: { baseAdapter: payload },
18371839
}),
18381840
});
1839-
const secondCall = alwaysValidateAdapter.validateAuthData.calls.argsFor(1);
1841+
const secondCall = baseAdapter.validateAuthData.calls.argsFor(1);
18401842
expect(secondCall[0]).toEqual(payload);
1841-
expect(secondCall[1]).toEqual(alwaysValidateAdapter);
1843+
expect(secondCall[1]).toEqual(baseAdapter);
18421844
expect(secondCall[2].config).toBeDefined();
18431845
expect(secondCall[2].auth).toBeDefined();
18441846
expect(secondCall[2].config.headers).toBeDefined();
@@ -1945,7 +1947,18 @@ describe('Auth Adapter features', () => {
19451947
expect(call[3].id).toEqual(user.id);
19461948
expect(user.getSessionToken()).toBeDefined();
19471949
});
1948-
xit('should throw if no triggers found');
1950+
it('should throw if no triggers found', async () => {
1951+
await reconfigureServer({ auth: { wrongAdapter } });
1952+
const user = new Parse.User();
1953+
try {
1954+
await user.save({ authData: { wrongAdapter: { id: 'wrongAdapter' } } });
1955+
fail('should throw');
1956+
} catch (e) {
1957+
expect(e.message).toContain(
1958+
'Adapter not ready, need to implement validateAuthData or (validateSetUp, validateLogin, validateUpdate)'
1959+
);
1960+
}
1961+
});
19491962
it('should not update authData if provider return doNotSave', async () => {
19501963
spyOn(doNotSaveAdapter, 'validateAuthData').and.resolveTo({ doNotSave: true });
19511964
await reconfigureServer({
@@ -1962,23 +1975,23 @@ describe('Auth Adapter features', () => {
19621975

19631976
expect(user.get('authData')).toEqual({ baseAdapter: { id: 'baseAdapter' } });
19641977
});
1965-
it('should force authData validation if provider use alwaysValidate', async () => {
1966-
spyOn(alwaysValidateAdapter, 'validateAuthData').and.resolveTo({});
1978+
it('should perform authData validation only when its required', async () => {
1979+
spyOn(baseAdapter2, 'validateAuthData').and.resolveTo({});
19671980
spyOn(baseAdapter, 'validateAuthData').and.resolveTo({});
19681981
await reconfigureServer({
1969-
auth: { alwaysValidateAdapter, baseAdapter },
1982+
auth: { baseAdapter2, baseAdapter },
19701983
});
19711984

19721985
const user = new Parse.User();
19731986

19741987
await user.save({
19751988
authData: {
19761989
baseAdapter: { id: 'baseAdapter' },
1977-
alwaysValidateAdapter: { token: true },
1990+
baseAdapter2: { token: true },
19781991
},
19791992
});
19801993

1981-
expect(alwaysValidateAdapter.validateAuthData).toHaveBeenCalledTimes(1);
1994+
expect(baseAdapter2.validateAuthData).toHaveBeenCalledTimes(1);
19821995

19831996
const user2 = new Parse.User();
19841997
await user2.save({
@@ -1987,17 +2000,17 @@ describe('Auth Adapter features', () => {
19872000
},
19882001
});
19892002

1990-
expect(alwaysValidateAdapter.validateAuthData).toHaveBeenCalledTimes(1);
2003+
expect(baseAdapter2.validateAuthData).toHaveBeenCalledTimes(1);
19912004

19922005
const user3 = new Parse.User();
19932006
await user3.save({
19942007
authData: {
19952008
baseAdapter: { id: 'baseAdapter' },
1996-
alwaysValidateAdapter: { token: true },
2009+
baseAdapter2: { token: true },
19972010
},
19982011
});
19992012

2000-
expect(alwaysValidateAdapter.validateAuthData).toHaveBeenCalledTimes(2);
2013+
expect(baseAdapter2.validateAuthData).toHaveBeenCalledTimes(2);
20012014
});
20022015
it('should require additional provider if configured', async () => {
20032016
await reconfigureServer({
@@ -2061,26 +2074,26 @@ describe('Auth Adapter features', () => {
20612074
spyOn(baseAdapter, 'validateAuthData').and.resolveTo({
20622075
response: { someData: true },
20632076
});
2064-
spyOn(alwaysValidateAdapter, 'validateAuthData').and.resolveTo({
2077+
spyOn(baseAdapter2, 'validateAuthData').and.resolveTo({
20652078
response: { someData2: true },
20662079
save: { otherData: true },
20672080
});
20682081
await reconfigureServer({
2069-
auth: { baseAdapter, alwaysValidateAdapter },
2082+
auth: { baseAdapter, baseAdapter2 },
20702083
});
20712084

20722085
const user = new Parse.User();
20732086

20742087
await user.save({
20752088
authData: {
20762089
baseAdapter: { id: 'baseAdapter' },
2077-
alwaysValidateAdapter: { test: true },
2090+
baseAdapter2: { test: true },
20782091
},
20792092
});
20802093

20812094
expect(user.get('authDataResponse')).toEqual({
20822095
baseAdapter: { someData: true },
2083-
alwaysValidateAdapter: { someData2: true },
2096+
baseAdapter2: { someData2: true },
20842097
});
20852098

20862099
const user2 = new Parse.User();
@@ -2089,46 +2102,46 @@ describe('Auth Adapter features', () => {
20892102
{
20902103
authData: {
20912104
baseAdapter: { id: 'baseAdapter' },
2092-
alwaysValidateAdapter: { test: true },
2105+
baseAdapter2: { test: true },
20932106
},
20942107
},
20952108
{ sessionToken: user.getSessionToken() }
20962109
);
20972110

2098-
expect(user2.get('authDataResponse')).toEqual({ alwaysValidateAdapter: { someData2: true } });
2111+
expect(user2.get('authDataResponse')).toEqual({ baseAdapter2: { someData2: true } });
20992112

21002113
const user3 = new Parse.User();
21012114
await user3.save({
21022115
authData: {
21032116
baseAdapter: { id: 'baseAdapter' },
2104-
alwaysValidateAdapter: { test: true },
2117+
baseAdapter2: { test: true },
21052118
},
21062119
});
21072120

21082121
// On logIn all authData are revalidated
21092122
expect(user3.get('authDataResponse')).toEqual({
21102123
baseAdapter: { someData: true },
2111-
alwaysValidateAdapter: { someData2: true },
2124+
baseAdapter2: { someData2: true },
21122125
});
21132126

21142127
const userViaMasterKey = new Parse.User();
21152128
userViaMasterKey.id = user2.id;
21162129
await userViaMasterKey.fetch({ useMasterKey: true });
21172130
expect(userViaMasterKey.get('authData')).toEqual({
21182131
baseAdapter: { id: 'baseAdapter' },
2119-
alwaysValidateAdapter: { otherData: true },
2132+
baseAdapter2: { otherData: true },
21202133
});
21212134
});
21222135
it('should return authData response and save some info on username login', async () => {
21232136
spyOn(baseAdapter, 'validateAuthData').and.resolveTo({
21242137
response: { someData: true },
21252138
});
2126-
spyOn(alwaysValidateAdapter, 'validateAuthData').and.resolveTo({
2139+
spyOn(baseAdapter2, 'validateAuthData').and.resolveTo({
21272140
response: { someData2: true },
21282141
save: { otherData: true },
21292142
});
21302143
await reconfigureServer({
2131-
auth: { baseAdapter, alwaysValidateAdapter },
2144+
auth: { baseAdapter, baseAdapter2 },
21322145
});
21332146

21342147
const user = new Parse.User();
@@ -2138,13 +2151,13 @@ describe('Auth Adapter features', () => {
21382151
password: 'password',
21392152
authData: {
21402153
baseAdapter: { id: 'baseAdapter' },
2141-
alwaysValidateAdapter: { test: true },
2154+
baseAdapter2: { test: true },
21422155
},
21432156
});
21442157

21452158
expect(user.get('authDataResponse')).toEqual({
21462159
baseAdapter: { someData: true },
2147-
alwaysValidateAdapter: { someData2: true },
2160+
baseAdapter2: { someData2: true },
21482161
});
21492162

21502163
const res = await request({
@@ -2155,36 +2168,36 @@ describe('Auth Adapter features', () => {
21552168
username: 'username',
21562169
password: 'password',
21572170
authData: {
2158-
alwaysValidateAdapter: { test: true },
2171+
baseAdapter2: { test: true },
21592172
baseAdapter: { id: 'baseAdapter' },
21602173
},
21612174
}),
21622175
});
21632176
const result = JSON.parse(res.text);
21642177
expect(result.authDataResponse).toEqual({
2165-
alwaysValidateAdapter: { someData2: true },
2178+
baseAdapter2: { someData2: true },
21662179
baseAdapter: { someData: true },
21672180
});
21682181

21692182
await user.fetch({ useMasterKey: true });
21702183
expect(user.get('authData')).toEqual({
21712184
baseAdapter: { id: 'baseAdapter' },
2172-
alwaysValidateAdapter: { otherData: true },
2185+
baseAdapter2: { otherData: true },
21732186
});
21742187
});
21752188
it('should allow update of authData', async () => {
21762189
spyOn(baseAdapter, 'validateAuthData').and.resolveTo({
21772190
response: { someData: true },
21782191
});
2179-
spyOn(alwaysValidateAdapter, 'validateAuthData').and.resolveTo({
2192+
spyOn(baseAdapter2, 'validateAuthData').and.resolveTo({
21802193
response: { someData2: true },
21812194
save: { otherData: true },
21822195
});
21832196
await reconfigureServer({
2184-
auth: { baseAdapter, alwaysValidateAdapter },
2197+
auth: { baseAdapter, baseAdapter2 },
21852198
});
21862199
await reconfigureServer({
2187-
auth: { baseAdapter, alwaysValidateAdapter },
2200+
auth: { baseAdapter, baseAdapter2 },
21882201
});
21892202

21902203
const user = new Parse.User();
@@ -2194,7 +2207,7 @@ describe('Auth Adapter features', () => {
21942207
password: 'password',
21952208
authData: {
21962209
baseAdapter: { id: 'baseAdapter' },
2197-
alwaysValidateAdapter: { test: true },
2210+
baseAdapter2: { test: true },
21982211
},
21992212
});
22002213
expect(baseAdapter.validateAuthData).toHaveBeenCalledTimes(1);
@@ -2206,7 +2219,7 @@ describe('Auth Adapter features', () => {
22062219
await user.save(
22072220
{
22082221
authData: {
2209-
alwaysValidateAdapter: { test: true },
2222+
baseAdapter2: { test: true },
22102223
baseAdapter: { id: 'baseAdapter' },
22112224
},
22122225
},
@@ -2220,7 +2233,7 @@ describe('Auth Adapter features', () => {
22202233
await user.save(
22212234
{
22222235
authData: {
2223-
alwaysValidateAdapter: { test: true },
2236+
baseAdapter2: { test: true },
22242237
baseAdapter: { id: 'baseAdapter' },
22252238
},
22262239
},
@@ -2233,7 +2246,7 @@ describe('Auth Adapter features', () => {
22332246
await user.save(
22342247
{
22352248
authData: {
2236-
alwaysValidateAdapter: { test: true },
2249+
baseAdapter2: { test: true },
22372250
baseAdapter: { id: 'baseAdapter2' },
22382251
},
22392252
},
@@ -2246,7 +2259,7 @@ describe('Auth Adapter features', () => {
22462259
await user.save(
22472260
{
22482261
authData: {
2249-
alwaysValidateAdapter: { test: true },
2262+
baseAdapter2: { test: true },
22502263
baseAdapter: { id: 'baseAdapter3' },
22512264
},
22522265
},
@@ -2258,12 +2271,12 @@ describe('Auth Adapter features', () => {
22582271
await user.fetch({ useMasterKey: true });
22592272
expect(user.get('authData')).toEqual({
22602273
baseAdapter: { id: 'baseAdapter3' },
2261-
alwaysValidateAdapter: { otherData: true },
2274+
baseAdapter2: { otherData: true },
22622275
});
22632276
});
22642277
it('should pass user to auth adapter on update by matching session', async () => {
2265-
spyOn(alwaysValidateAdapter, 'validateAuthData').and.resolveTo({});
2266-
await reconfigureServer({ auth: { alwaysValidateAdapter } });
2278+
spyOn(baseAdapter2, 'validateAuthData').and.resolveTo({});
2279+
await reconfigureServer({ auth: { baseAdapter2 } });
22672280

22682281
const user = new Parse.User();
22692282

@@ -2277,24 +2290,24 @@ describe('Auth Adapter features', () => {
22772290
expect(user.getSessionToken()).toBeDefined();
22782291

22792292
await user.save(
2280-
{ authData: { alwaysValidateAdapter: payload } },
2293+
{ authData: { baseAdapter2: payload } },
22812294
{ sessionToken: user.getSessionToken() }
22822295
);
22832296

2284-
const firstCall = alwaysValidateAdapter.validateAuthData.calls.argsFor(0);
2297+
const firstCall = baseAdapter2.validateAuthData.calls.argsFor(0);
22852298
expect(firstCall[0]).toEqual(payload);
2286-
expect(firstCall[1]).toEqual(alwaysValidateAdapter);
2299+
expect(firstCall[1]).toEqual(baseAdapter2);
22872300
expect(firstCall[2].config).toBeDefined();
22882301
expect(firstCall[2].auth).toBeDefined();
22892302
expect(firstCall[2].config.headers).toBeDefined();
22902303
expect(firstCall[3] instanceof Parse.User).toBeTruthy();
22912304
expect(firstCall[3].id).toEqual(user.id);
22922305

2293-
await user.save({ authData: { alwaysValidateAdapter: payload } }, { useMasterKey: true });
2306+
await user.save({ authData: { baseAdapter2: payload } }, { useMasterKey: true });
22942307

2295-
const secondCall = alwaysValidateAdapter.validateAuthData.calls.argsFor(1);
2308+
const secondCall = baseAdapter2.validateAuthData.calls.argsFor(1);
22962309
expect(secondCall[0]).toEqual(payload);
2297-
expect(secondCall[1]).toEqual(alwaysValidateAdapter);
2310+
expect(secondCall[1]).toEqual(baseAdapter2);
22982311
expect(secondCall[2].config).toBeDefined();
22992312
expect(secondCall[2].auth).toBeDefined();
23002313
expect(secondCall[2].config.headers).toBeDefined();

src/Adapters/Auth/AuthAdapter.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
/*eslint no-unused-vars: "off"*/
22
export class AuthAdapter {
33
constructor() {
4-
/**
5-
* Force authData validation even if authData are the same into the DB
6-
*/
7-
this.alwaysValidate = false;
84
/**
95
* Usage policy
106
* default: can be combined with ONE additional auth provider if additional configured on user

0 commit comments

Comments
 (0)