|
| 1 | +const { expect } = require('chai'); |
| 2 | +const fs = require('node:fs'); |
| 3 | +const { |
| 4 | + cleanupProject, |
| 5 | + initProject, |
| 6 | + runChtConf, |
| 7 | + getProjectDirectory |
| 8 | +} = require('./cht-conf-utils'); |
| 9 | +const path = require('path'); |
| 10 | + |
| 11 | +const FORMS = { |
| 12 | + 'app/death_report.xlsx': '1azFHCMTMehxuSg_LWOgsJZxrJo0yhseLp9FK4gTT38M', |
| 13 | + 'contact/person-create.xlsx': '1VhOPphx4IXyZiGbcevVZwvsvap8WPKHmUjOp8NuWJW8' |
| 14 | +}; |
| 15 | + |
| 16 | +/** |
| 17 | + * Running this test locally requires OAuth desktop client credentials for a Google Cloud Project. |
| 18 | + * https://docs.communityhealthtoolkit.org/apps/guides/forms/google-drive/ |
| 19 | + * Once you have created the OAuth client, configure the following environment variables in your current shell: |
| 20 | + * `CI=true`, `GOOGLE_REDIRECT_URI=http://localhost`, and `GOOGLE_CLIENT_ID=<client_id from OAuth client>`, |
| 21 | + * `GOOGLE_CLIENT_SECRET=<>`. Next, generate a `.gdrive.session.json` file with a valid access token by manually |
| 22 | + * running `cht fetch-forms-from-google-drive`. Copy the `.gdrive.session.json` file to the root of the cht-conf repo. |
| 23 | + * Then you should be able to successfully run `npm test-e2e`. |
| 24 | + * |
| 25 | + * On CI, this test runs with a configured Service Account by preemptively getting an access token and setting it in |
| 26 | + * the `.gdrive.session.json` file. Unfortunately, this means these tests cannot cover the login portion of the |
| 27 | + * functionality, but it does to cover the actual fetching and file writing logic. |
| 28 | + */ |
| 29 | +describe('fetch-forms-from-google-drive', () => { |
| 30 | + before(initProject); |
| 31 | + after(cleanupProject); |
| 32 | + |
| 33 | + it('downloads configured forms from Google Drive', async () => { |
| 34 | + const projectDir = getProjectDirectory(); |
| 35 | + await fs.promises.writeFile( |
| 36 | + path.join(projectDir, 'forms-on-google-drive.json'), |
| 37 | + JSON.stringify(FORMS, null, 2), |
| 38 | + ); |
| 39 | + |
| 40 | + const sessionJsonSrcPath = path.resolve(__dirname, '../../.gdrive.session.json'); |
| 41 | + const sessionJsonDestPath = path.resolve(projectDir, '.gdrive.session.json'); |
| 42 | + await fs.promises.copyFile(sessionJsonSrcPath, sessionJsonDestPath); |
| 43 | + |
| 44 | + await runChtConf('fetch-forms-from-google-drive'); |
| 45 | + |
| 46 | + const formFilePaths = Object |
| 47 | + .keys(FORMS) |
| 48 | + .map(formPath => path.join(projectDir, 'forms', formPath)); |
| 49 | + formFilePaths.forEach(formPath => { |
| 50 | + expect(fs.existsSync(formPath)).to.be.true; |
| 51 | + }); |
| 52 | + }); |
| 53 | +}); |
0 commit comments