-
Notifications
You must be signed in to change notification settings - Fork 114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
🐛 Fix AppConfig survey_info
backwards compat + add tests for dynamicConfig
#1115
Merged
shankari
merged 11 commits into
e-mission:service_rewrite_2023
from
JGreenlee:fix-surveyconfig-backwardscompat
Jan 20, 2024
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
3367e60
make labelVars as optional in AppConfig type def
JGreenlee 8c31b65
fix backwards compat on retrieved configs
JGreenlee 0527569
dynamicConfig: use 'function' syntax for big fns
JGreenlee b011bca
add JSdoc and types to fns in dynamicConfigs.ts
JGreenlee 113ed82
handle errors better while fetching/loading config
JGreenlee a99af4d
mockBEMUserCache: putRWDocument for storing config
JGreenlee b6a01f1
config backwards compats as one function
JGreenlee 6676dc5
only use fakeConfig if passed to mockBEMUserCache
JGreenlee 870bee2
initial tests for dynamicConfig.test.ts
JGreenlee 9bc5cd6
update dynamicConfig.test.ts
JGreenlee 63732f0
fix handling of invalid opcode
JGreenlee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
import { mockBEMUserCache } from '../__mocks__/cordovaMocks'; | ||
import { mockAlert, mockLogger } from '../__mocks__/globalMocks'; | ||
import { getConfig, initByUser } from '../js/config/dynamicConfig'; | ||
|
||
import initializedI18next from '../js/i18nextInit'; | ||
import { storageClear } from '../js/plugin/storage'; | ||
window['i18next'] = initializedI18next; | ||
|
||
mockLogger(); | ||
mockAlert(); | ||
mockBEMUserCache(); | ||
|
||
beforeEach(() => { | ||
// clear all storage and the config document | ||
storageClear({ local: true, native: true }); | ||
window['cordova'].plugins.BEMUserCache.putRWDocument('config/app_ui_config', {}); | ||
}); | ||
|
||
const nrelCommuteConfig = { | ||
version: 1, | ||
server: { | ||
connectUrl: 'https://nrel-commute-openpath.nrel.gov/api/', | ||
aggregate_call_auth: 'user_only', | ||
}, | ||
// ... | ||
}; | ||
|
||
const denverCasrConfig = { | ||
version: 1, | ||
server: { | ||
connectUrl: 'https://denver-casr-openpath.nrel.gov/api/', | ||
aggregate_call_auth: 'user_only', | ||
}, | ||
opcode: { | ||
autogen: true, | ||
subgroups: [ | ||
'test', | ||
'qualified-cargo', | ||
'qualified-regular', | ||
'standard-cargo', | ||
'standard-regular', | ||
], | ||
}, | ||
// ... | ||
}; | ||
|
||
global.fetch = (url: string) => { | ||
return new Promise((rs, rj) => { | ||
if (url.includes('nrel-commute.nrel-op.json')) { | ||
rs({ | ||
ok: true, | ||
json: () => new Promise((rs, rj) => rs(nrelCommuteConfig)), | ||
}); | ||
} else if (url.includes('denver-casr.nrel-op.json')) { | ||
rs({ | ||
ok: true, | ||
json: () => new Promise((rs, rj) => rs(denverCasrConfig)), | ||
}); | ||
} else { | ||
rj(new Error('404 while fetching ' + url)); | ||
} | ||
}) as any; | ||
}; | ||
|
||
describe('dynamicConfig', () => { | ||
const fakeStudyName = 'gotham-city-transit'; | ||
const validStudyNrelCommute = 'nrel-commute'; | ||
const validStudyDenverCasr = 'denver-casr'; | ||
|
||
describe('getConfig', () => { | ||
it('should resolve with null since no config is set yet', async () => { | ||
await expect(getConfig()).resolves.toBeNull(); | ||
}); | ||
it('should resolve with a valid config once initByUser is called for an nrel-commute token', async () => { | ||
const validToken = `nrelop_${validStudyNrelCommute}_user1`; | ||
await initByUser({ token: validToken }); | ||
const config = await getConfig(); | ||
expect(config.server.connectUrl).toBe('https://nrel-commute-openpath.nrel.gov/api/'); | ||
expect(config.joined).toEqual({ | ||
opcode: validToken, | ||
study_name: validStudyNrelCommute, | ||
subgroup: undefined, | ||
}); | ||
}); | ||
|
||
it('should resolve with a valid config once initByUser is called for a denver-casr token', async () => { | ||
const validToken = `nrelop_${validStudyDenverCasr}_test_user1`; | ||
await initByUser({ token: validToken }); | ||
const config = await getConfig(); | ||
expect(config.server.connectUrl).toBe('https://denver-casr-openpath.nrel.gov/api/'); | ||
expect(config.joined).toEqual({ | ||
opcode: validToken, | ||
study_name: validStudyDenverCasr, | ||
subgroup: 'test', | ||
}); | ||
}); | ||
}); | ||
|
||
describe('initByUser', () => { | ||
// fake study (gotham-city-transit) | ||
it('should error if the study is nonexistent', async () => { | ||
const fakeBatmanToken = `nrelop_${fakeStudyName}_batman`; | ||
await expect(initByUser({ token: fakeBatmanToken })).rejects.toThrow(); | ||
}); | ||
|
||
// real study without subgroups (nrel-commute) | ||
it('should error if the study exists but the token is invalid format', async () => { | ||
const badToken1 = validStudyNrelCommute; // doesn't start with nrelop_ | ||
await expect(initByUser({ token: badToken1 })).rejects.toThrow(); | ||
const badToken2 = `nrelop_${validStudyNrelCommute}`; // doesn't have enough _ | ||
await expect(initByUser({ token: badToken2 })).rejects.toThrow(); | ||
const badToken3 = `nrelop_${validStudyNrelCommute}_`; // doesn't have user code after last _ | ||
await expect(initByUser({ token: badToken3 })).rejects.toThrow(); | ||
}); | ||
it('should return true after successfully storing the config for a valid token', async () => { | ||
const validToken = `nrelop_${validStudyNrelCommute}_user2`; | ||
await expect(initByUser({ token: validToken })).resolves.toBe(true); | ||
}); | ||
|
||
// real study with subgroups (denver-casr) | ||
it('should error if the study uses subgroups but the token has no subgroup', async () => { | ||
const tokenWithoutSubgroup = `nrelop_${validStudyDenverCasr}_user2`; | ||
await expect(initByUser({ token: tokenWithoutSubgroup })).rejects.toThrow(); | ||
}); | ||
it('should error if the study uses subgroups and the token is invalid format', async () => { | ||
const badToken1 = `nrelop_${validStudyDenverCasr}_test_`; // doesn't have user code after last _ | ||
await expect(initByUser({ token: badToken1 })).rejects.toThrow(); | ||
}); | ||
it('should return true after successfully storing the config for a valid token with subgroup', async () => { | ||
const validToken = `nrelop_${validStudyDenverCasr}_test_user2`; | ||
await expect(initByUser({ token: validToken })).resolves.toBe(true); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not your change, but I would like to understand this better. Why do we pass in the config instead of calling
putRWDocument