Skip to content

Commit

Permalink
Add expo-secure-store (#3325)
Browse files Browse the repository at this point in the history
  • Loading branch information
retyui authored and AndrewSouthpaw committed May 17, 2019
1 parent 259dafe commit 49c5ee2
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 0 deletions.
@@ -0,0 +1,50 @@
declare module 'expo-secure-store' {
// Types
// `KA$` is Keychain Accessible namespace
declare export opaque type KA$AFTER_FIRST_UNLOCK: number;
declare export opaque type KA$AFTER_FIRST_UNLOCK_THIS_DEVICE_ONLY: number;
declare export opaque type KA$ALWAYS: number;
declare export opaque type KA$WHEN_PASSCODE_SET_THIS_DEVICE_ONLY: number;
declare export opaque type KA$ALWAYS_THIS_DEVICE_ONLY: number;
declare export opaque type KA$WHEN_UNLOCKED: number;
declare export opaque type KA$WHEN_UNLOCKED_THIS_DEVICE_ONLY: number;
declare export type CommonSecureStoreOptions = {|
keychainService?: string,
|};
declare export type SecureStoreOptions = {|
...CommonSecureStoreOptions,
keychainAccessible?:
| KA$AFTER_FIRST_UNLOCK
| KA$AFTER_FIRST_UNLOCK_THIS_DEVICE_ONLY
| KA$ALWAYS
| KA$WHEN_PASSCODE_SET_THIS_DEVICE_ONLY
| KA$ALWAYS_THIS_DEVICE_ONLY
| KA$WHEN_UNLOCKED
| KA$WHEN_UNLOCKED_THIS_DEVICE_ONLY,
|};

// Variables
declare export var AFTER_FIRST_UNLOCK: KA$AFTER_FIRST_UNLOCK;
declare export var AFTER_FIRST_UNLOCK_THIS_DEVICE_ONLY: KA$AFTER_FIRST_UNLOCK_THIS_DEVICE_ONLY;
declare export var ALWAYS: KA$ALWAYS;
declare export var WHEN_PASSCODE_SET_THIS_DEVICE_ONLY: KA$WHEN_PASSCODE_SET_THIS_DEVICE_ONLY;
declare export var ALWAYS_THIS_DEVICE_ONLY: KA$ALWAYS_THIS_DEVICE_ONLY;
declare export var WHEN_UNLOCKED: KA$WHEN_UNLOCKED;
declare export var WHEN_UNLOCKED_THIS_DEVICE_ONLY: KA$WHEN_UNLOCKED_THIS_DEVICE_ONLY;

declare export function deleteItemAsync(
key: string,
options?: CommonSecureStoreOptions
): Promise<void>;

declare export function getItemAsync(
key: string,
options?: CommonSecureStoreOptions
): Promise<string | null>;

declare export function setItemAsync(
key: string,
value: string,
options?: SecureStoreOptions
): Promise<void>;
}
@@ -0,0 +1,98 @@
// @flow
import { it, describe } from 'flow-typed-test';

import {
AFTER_FIRST_UNLOCK,
AFTER_FIRST_UNLOCK_THIS_DEVICE_ONLY,
ALWAYS,
WHEN_PASSCODE_SET_THIS_DEVICE_ONLY,
ALWAYS_THIS_DEVICE_ONLY,
WHEN_UNLOCKED,
WHEN_UNLOCKED_THIS_DEVICE_ONLY,
deleteItemAsync,
getItemAsync,
setItemAsync,
} from 'expo-secure-store';

describe('deleteItemAsync', () => {
it('should passes when used properly', () => {
deleteItemAsync('KEY').then(result => {
(result: void);

// $ExpectError: check any
(result: number);
});
deleteItemAsync('KEY', {});
deleteItemAsync('KEY', { keychainService: 'kService' });
});

it('should raises an error when pass invalid arguments', () => {
// $ExpectError: first argument is required
deleteItemAsync();
// $ExpectError: first argument must be a number
deleteItemAsync(69);
// $ExpectError: second argument must be an object
deleteItemAsync('', 69);
});
});

describe('getItemAsync', () => {
it('should passes when used properly', () => {
getItemAsync('KEY').then(result => {
if (result !== null) {
(result: string);
} else {
(result: null);
}

// $ExpectError: check any
(result: number);
});
getItemAsync('KEY', {});
getItemAsync('KEY', { keychainService: 'kService' });
});

it('should raises an error when pass invalid arguments', () => {
// $ExpectError: first argument is required
getItemAsync();
// $ExpectError: first argument must be a number
getItemAsync(69);
// $ExpectError: second argument must be an object
getItemAsync('', 69);
});
});

describe('setItemAsync', () => {
it('should passes when used properly', () => {
setItemAsync('KEY', 'VALUE').then(result => {
(result: void);

// $ExpectError: check any
(result: number);
});
setItemAsync('KEY', 'VALUE', {});
setItemAsync('KEY', 'VALUE', { keychainService: 'kService' });
setItemAsync('KEY', 'VALUE', {
keychainService: 'kService',
keychainAccessible: AFTER_FIRST_UNLOCK,
});
});

it('should raises an error when pass invalid arguments', () => {
// $ExpectError: first argument is required
setItemAsync();
// $ExpectError: first argument must be a number
setItemAsync(69);
// $ExpectError: second argument is required
setItemAsync('KEY');
// $ExpectError: second argument is must be a string
setItemAsync('KEY', 69);
// $ExpectError: third argument is must be an object
setItemAsync('KEY', 69);

setItemAsync('KEY', 'VALUE', {
// $ExpectError: invalid value
keychainAccessible: 1,
});
});
});

0 comments on commit 49c5ee2

Please sign in to comment.