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
Adds support for google drive backups on android #22
Merged
tanx
merged 3 commits into
photon-sdk:master
from
ConorOkus:android-google-drive-support
Jan 7, 2021
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import * as CloudStore from '../../src/cloudstore'; | ||
import * as GDriveCloudStorage from '../../src/GDriveCloudStorage'; | ||
|
||
describe('CloudStore android unit test', () => { | ||
const keyId = '8abe1a93-6a9c-490c-bbd5-d7f11a4a9c8f'; | ||
const ciphertext = Buffer.from('encrypted stuff'); | ||
|
||
describe('putKey', () => { | ||
it('fail on invalid args', async () => { | ||
const setItemSpy = jest.spyOn(GDriveCloudStorage, 'setItem'); | ||
|
||
await expect(CloudStore.putKey({ keyId })).rejects.toThrow(/Invalid/); | ||
expect(setItemSpy.mock.calls.length).toBe(0); | ||
}); | ||
|
||
it('store item', async () => { | ||
const getItemSpy = jest.spyOn(GDriveCloudStorage, 'getItem').mockImplementation(); | ||
const setItemSpy = jest.spyOn(GDriveCloudStorage, 'setItem'); | ||
|
||
await CloudStore.putKey({ keyId, ciphertext }); | ||
expect(setItemSpy.mock.calls[0][0]).toBe('1_photon_key_id'); | ||
expect(setItemSpy.mock.calls[0][1]).toBe(keyId); | ||
expect(setItemSpy.mock.calls[1][0]).toBe('1_8abe1a93'); | ||
expect(setItemSpy.mock.calls[1][1]).toMatch(/^{"keyId":.*"}$/); | ||
expect(setItemSpy.mock.calls.length).toBe(2); | ||
|
||
getItemSpy.mockRestore(); | ||
setItemSpy.mockRestore(); | ||
}); | ||
|
||
it('should not backup twice', async () => { | ||
const getItemSpy = jest.spyOn(GDriveCloudStorage, 'getItem').mockResolvedValue('resolved value'); | ||
const setItemSpy = jest.spyOn(GDriveCloudStorage, 'setItem'); | ||
|
||
await expect(CloudStore.putKey({ keyId, ciphertext })).rejects.toThrow(/already present/); | ||
getItemSpy.mockRestore(); | ||
|
||
await CloudStore.putKey({ keyId, ciphertext }); | ||
expect(setItemSpy.mock.calls.length).toBe(2); | ||
|
||
setItemSpy.mockRestore(); | ||
}); | ||
}); | ||
|
||
describe('getKey', () => { | ||
it('should not find item', async () => { | ||
const getItemSpy = jest.spyOn(GDriveCloudStorage, 'getItem').mockResolvedValue(null); | ||
|
||
const stored = await CloudStore.getKey(); | ||
expect(stored).toBe(null); | ||
|
||
getItemSpy.mockRestore(); | ||
}); | ||
|
||
it('should get stored item by key Id', async () => { | ||
const result = { | ||
keyId: keyId, | ||
ciphertext: ciphertext, | ||
time: new Date(), | ||
}; | ||
|
||
const getItemSpy = jest | ||
.spyOn(GDriveCloudStorage, 'getItem') | ||
.mockImplementation() | ||
.mockResolvedValueOnce(keyId) | ||
.mockResolvedValueOnce(JSON.stringify(result)); | ||
|
||
const stored = await CloudStore.getKey(); | ||
expect(stored).toEqual({ | ||
keyId, | ||
ciphertext, | ||
time: expect.objectContaining(new Date()), | ||
}); | ||
|
||
getItemSpy.mockRestore(); | ||
}); | ||
|
||
describe('removeKeyId', () => { | ||
it('fail on invalid args', async () => { | ||
const result = { | ||
keyId: keyId, | ||
ciphertext: ciphertext, | ||
time: new Date(), | ||
}; | ||
|
||
const removeItemSpy = jest.spyOn(GDriveCloudStorage, 'removeItem'); | ||
const getItemSpy = jest.spyOn(GDriveCloudStorage, 'getItem').mockResolvedValueOnce(keyId).mockResolvedValue(JSON.stringify(result)); | ||
const getKeySpy = jest.spyOn(CloudStore, 'getKey').mockResolvedValue(result); | ||
|
||
await expect(CloudStore.removeKeyId({ keyId: 'invalid' })).rejects.toThrow(/not found/); | ||
expect(getItemSpy.mock.calls.length).toBe(2); | ||
|
||
getItemSpy.mockRestore(); | ||
removeItemSpy.mockRestore(); | ||
getKeySpy.mockRestore(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
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,14 @@ | ||
{ | ||
"preset": "react-native", | ||
"transform": { | ||
"^.+\\.js$": "<rootDir>/node_modules/react-native/jest/preprocessor.js" | ||
}, | ||
"haste": { | ||
"defaultPlatform": "android", | ||
"platforms": ["android"] | ||
}, | ||
"watchPathIgnorePatterns": [ | ||
"<rootDir>/node_modules" | ||
], | ||
"setupFiles": ["./__tests__/setup.js"] | ||
} |
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,66 @@ | ||
import GDrive from 'react-native-google-drive-api-wrapper'; | ||
import { GoogleSignin, statusCodes } from '@react-native-community/google-signin'; | ||
|
||
export async function authenticate(options = {}) { | ||
GoogleSignin.configure({ | ||
scopes: ['https://www.googleapis.com/auth/drive.appdata'], | ||
...options, | ||
}); | ||
await GoogleSignin.hasPlayServices({ | ||
showPlayServicesUpdateDialog: true, | ||
}); | ||
try { | ||
await GoogleSignin.signInSilently(); | ||
} catch (error) { | ||
if (error.code === statusCodes.SIGN_IN_REQUIRED) { | ||
await GoogleSignin.signIn(); | ||
} | ||
} | ||
const { accessToken } = await GoogleSignin.getTokens(); | ||
GDrive.setAccessToken(accessToken); | ||
GDrive.init(); | ||
if (!GDrive.isInitialized) { | ||
throw new Error('Unable to use GDrive'); | ||
} | ||
} | ||
|
||
export async function setItem(keyId, value) { | ||
const content = Buffer.from(value).toString('base64'); | ||
await GDrive.files.createFileMultipart( | ||
content, | ||
'text/plain', | ||
{ | ||
parents: ['appDataFolder'], | ||
name: keyId, | ||
}, | ||
true, | ||
); | ||
} | ||
|
||
export async function _getFileId(keyId) { | ||
const response = await GDrive.files.list({ spaces: 'appDataFolder', fields: 'nextPageToken, files(id, name)' }); | ||
const json = await response.json(); | ||
if (json.files.length === 0) { | ||
return null; | ||
} | ||
const file = json.files.find(file => file.name === keyId); | ||
return file ? file.id : null; | ||
} | ||
|
||
export async function getItem(keyId) { | ||
const fileId = await _getFileId(keyId); | ||
if (!fileId) { | ||
return null; | ||
} | ||
const response = await GDrive.files.get(fileId, { alt: 'media' }); | ||
const key = await response.text(); | ||
return key; | ||
} | ||
|
||
export async function removeItem(keyId) { | ||
const fileId = await _getFileId(keyId); | ||
if (!fileId) { | ||
return null; | ||
} | ||
await GDrive.files.delete(fileId); | ||
} |
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
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.
Nice 👍 Does jest emulate an Android environment if the file is called
*.android.test.js
or how does it work?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.
No unfortunately not, the naming convention just makes it a bit easier to run in isolation here it's not ideal to have a setup file for running in the android environment but changing the Platform directly in the tests doesn't seem to work so this is a workaround using Haste.