Browse Source

return keystore outcomes

pull/1/head
Tim Glasgow 2 years ago
parent
commit
7bd2ab24bf
  1. 8
      squarenotsquare/__mock__/mockKeyStore.js
  2. 38
      squarenotsquare/__tests__/Services-test.js
  3. 16
      squarenotsquare/src/services/Keystore.js

8
squarenotsquare/__mock__/mockKeyStore.js

@ -1,9 +1,15 @@
class MockKeyStore {
async remove(key) {
if (key === 'notakey') {
throw ('Key not found');
}
return true;
}
async set(key, value) {
if (key === 'savefailed'){
throw ('Failed to save key');
}
return true;
}
@ -12,6 +18,8 @@ class MockKeyStore {
return null;
} else if (key === 'negTest') {
return '-1';
} else if (key === 'notakey') {
throw ('Key not found');
} else {
return '1234567890';
}

38
squarenotsquare/__tests__/Services-test.js

@ -1,13 +1,43 @@
import { removeKey, setKey, getKey } from "../src/services/Keystore";
test('calls set keystore key', () => {
expect(setKey.bind('100', '100')).not.toThrow();
})
return setKey('100', '100')
.then((outcome) => {
expect(outcome).toEqual(true);
});
});
test('calls get keystore key', () => {
expect(getKey.bind('100')).not.toThrow();
return getKey('100')
.then((outcome) => {
expect(outcome).toEqual('1234567890');
});
})
test('calls remove keystore key', () => {
expect(removeKey.bind('100')).not.toThrow();
return removeKey('100')
.then((outcome) => {
expect(outcome).toEqual(true);
});
})
test('get non-existing keystore key', () => {
return getKey('notakey')
.then((outcome) => {
expect(outcome).toEqual(null);
});
})
test('remove non-existing keystore key', () => {
return removeKey('notakey')
.then((outcome) => {
expect(outcome).toEqual(false);
});
})
test('setKey handles case where settings fails', () => {
return setKey('savefailed')
.then((outcome) => {
expect(outcome).toEqual(false);
});
})

16
squarenotsquare/src/services/Keystore.js

@ -1,27 +1,33 @@
import RNSecureKeyStore, {ACCESSIBLE} from 'react-native-secure-key-store';
export async function removeKey(key) {
let outcome = null;
await RNSecureKeyStore.remove(key).then(
(res) => {
console.log('Removed key ' + res);
outcome = true;
},
(err) => {
console.log(err.message);
outcome = false;
},
);
return outcome;
}
export async function setKey(key, value) {
let outcome = null;
await RNSecureKeyStore.set(key, value, {
accessible: ACCESSIBLE.WHEN_UNLOCKED_THIS_DEVICE_ONLY,
}).then(
(res) => {
console.log('Set key ' + res);
outcome = true;
},
(err) => {
console.log(err.message);
outcome = false;
},
);
return outcome;
}
export async function getKey(key) {
@ -31,7 +37,7 @@ export async function getKey(key) {
value = res;
},
(err) => {
console.log(err.message);
value = null;
},
);
return value;

Loading…
Cancel
Save