Files
iNaturalistReactNative/__mocks__/react-native-sensitive-info.ts
Johannes Klein 25c13c9eb5 Reapply react-native-sensitive-info update with "migration" (#3373)
* Reapply "Update react-native-sensitive-info to 5.6.2 (#3346)"

This reverts commit 4bd596074c.

* Explicitly set keychainService to "app" for all calls to RNSInfo

So, apparently the default string used to save sensitive items changed from "app" to "default" between versions 6.0.0-alpha.9 and 5.6.2.
So if we install an app update built with 5.6.2 on top of our previous version (using 6.0.0-alpha.9) we can no longer find our previously stored secrets.
I am leaving the key as overridable by actual options propped in, in case someone wants to build a separate slice at one point. In contrast to accessControl which probably should not be a decision in engineerings court.

* Add comment

* Update test helpers
2026-02-19 22:36:20 +01:00

115 lines
3.0 KiB
TypeScript

let clearAuthCache = () => {}; // Default no-op function
// Try to get clearAuthCache function safely
try {
const authModule = require( "components/LoginSignUp/AuthenticationService" );
if ( authModule && typeof authModule.clearAuthCache === "function" ) {
// eslint-disable-next-line prefer-destructuring
clearAuthCache = authModule.clearAuthCache;
} else if ( authModule.default && typeof authModule.default.clearAuthCache === "function" ) {
// eslint-disable-next-line prefer-destructuring
clearAuthCache = authModule.default.clearAuthCache;
}
} catch ( error ) {
console.warn( "Could not import clearAuthCache, using no-op function", error );
}
class RNSInfo {
static stores = new Map();
static getServiceName( o = {} ) {
return o.sharedPreferencesName
|| o.keychainService
|| "default";
}
static validateString( s ) {
if ( typeof s !== "string" ) { throw new Error( "Invalid string:", s ); }
}
static clearAllStores = jest.fn( () => {
RNSInfo.stores.clear();
clearAuthCache();
} );
static clearAuthCache = jest.fn( () => {
clearAuthCache();
} );
static hasItem = jest.fn( async ( k, o ) => {
RNSInfo.validateString( k );
const serviceName = RNSInfo.getServiceName( o );
const service = RNSInfo.stores.get( serviceName );
if ( service ) { return service.has( k ); }
return false;
} );
static getItem = jest.fn( async ( k, o ) => {
RNSInfo.validateString( k );
const serviceName = RNSInfo.getServiceName( o );
const service = RNSInfo.stores.get( serviceName );
if ( service ) { return service.get( k ) || null; }
return null;
} );
static getAllItems = jest.fn( async o => {
const serviceName = RNSInfo.getServiceName( o );
const service = RNSInfo.stores.get( serviceName );
let mappedValues = [];
if ( service?.size ) {
mappedValues = Array.from( service.entries() ).map(
( [key, value] ) => ( { key, value, service: serviceName } ),
);
}
return mappedValues;
} );
static setItem = jest.fn( async ( k, v, o ) => {
RNSInfo.validateString( k );
RNSInfo.validateString( v );
const serviceName = RNSInfo.getServiceName( o );
let service = RNSInfo.stores.get( serviceName );
if ( !service ) {
RNSInfo.stores.set( serviceName, new Map() );
service = RNSInfo.stores.get( serviceName );
}
service.set( k, v );
clearAuthCache( );
return null;
} );
static deleteItem = jest.fn( async ( k, o ) => {
RNSInfo.validateString( k );
const serviceName = RNSInfo.getServiceName( o );
const service = RNSInfo.stores.get( serviceName );
if ( service ) { service.delete( k ); }
clearAuthCache( );
return null;
} );
static hasEnrolledFingerprints = jest.fn( async () => true );
static setInvalidatedByBiometricEnrollment = jest.fn();
// "Touch ID" | "Face ID" | false
static isSensorAvailable = jest.fn( async () => "Face ID" );
}
module.exports = RNSInfo;