feat: 🎸 Add sqlite support for auth methods (#2969)

✅ Closes: https://hashicorp.atlassian.net/browse/ICU-17418
This commit is contained in:
Zhihe Li committed 2025-09-03 11:41:42 -04:00
1 parent ba7b8b8824
commit c2b0f321de
17 files changed
+227 -30

No files matched your search

@@ -73,17 +73,15 @@ module('Unit | Handler | sqlite-handler', function (hooks) {
sqliteHandler,
store,
applicationAdapter,
sqliteHandlerSpy,
applicationAdapterSpy;
sqliteHandlerSpy;
hooks.beforeEach(async function setupSqliteHandler() {
store = this.owner.lookup('service:store');
manager = new RequestManager();
sqliteHandler = new SqliteHandler(store);
applicationAdapter = store.adapterFor('application');
// spy on all handler and application adapter methods
// spy on handler methods
sqliteHandlerSpy = sinon.spy(sqliteHandler);
applicationAdapterSpy = sinon.spy(applicationAdapter);
store.requestManager = manager;
manager.use([sqliteHandler]);
@@ -94,6 +92,10 @@ module('Unit | Handler | sqlite-handler', function (hooks) {
});
});
hooks.afterEach(async function cleanUpSqliteHandler() {
sinon.restore();
});
module('fetching and batch loading', function (hooks) {
// `testBatchLimit` and `serverResultPageLimit` are intentionally difference
// so that multiple calls to the server are required to fetch a single batch,
@@ -137,6 +139,8 @@ module('Unit | Handler | sqlite-handler', function (hooks) {
});
test('supports batching requests and writes to SQLite', async function (assert) {
const applicationAdapterSpy = sinon.spy(applicationAdapter);
await store.query('target', {});
assert.strictEqual(
@@ -197,6 +201,36 @@ module('Unit | Handler | sqlite-handler', function (hooks) {
);
assert.strictEqual(callCounts.writeToDb, expectedWriteToSqliteCallCount);
});
test('it stores and retrieves tokens when storeToken is true (default)', async function (assert) {
const sqliteSpy = sinon.spy(sqliteHandler.sqlite);
await store.query('target', {});
assert.strictEqual(
sqliteSpy.insertResource.withArgs('token').callCount,
targets.length / testBatchLimit,
);
assert.strictEqual(
sqliteSpy.insertResource.withArgs('target').callCount,
targets.length / testBatchLimit,
);
});
test('it clears database and does not store tokens when storeToken is false', async function (assert) {
const sqliteSpy = sinon.spy(sqliteHandler.sqlite);
await store.query('target', {}, { storeToken: false });
// Targets are all deleted one time
assert.ok(sqliteSpy.deleteResource.calledOnceWithExactly('target'));
assert.ok(sqliteSpy.insertResource.withArgs('token').notCalled);
assert.strictEqual(
sqliteSpy.insertResource.withArgs('target').callCount,
targets.length / testBatchLimit,
);
});
});
module('option: pushToStore', function (hooks) {
@@ -435,4 +469,58 @@ module('Unit | Handler | sqlite-handler', function (hooks) {
assert.strictEqual(results[0].name, 'specific-target');
});
test('it retries query when invalid list token error occurs', async function (assert) {
let targets = this.server.createList('target', 5, { scope });
const sqliteSpy = sinon.spy(sqliteHandler.sqlite);
// Mock the first query to fail with invalid list token error but return data as normal otherwise
sinon
.stub(applicationAdapter, 'query')
.callThrough()
.onFirstCall()
.throws(() => {
const error = new Error('Invalid list token');
error.errors = [
{
status: 400,
code: 'invalid list token',
},
];
return error;
});
this.server.get(
'targets',
createPaginatedResponseHandler.call(this, targets, {
pageSize: 5,
}),
);
// Insert tokens to simulate existing state
await sqliteHandler.sqlite.insertResource('token', [
['target-123', 'invalid key'],
['target-321', 'valid key'],
['session-123', 'session key'],
]);
const results = await store.query('target', {});
assert.ok(sqliteHandlerSpy.retryQueryFailure.calledOnce);
assert.ok(sqliteSpy.deleteResource.calledWithExactly('target'));
assert.ok(
sqliteSpy.deleteResource.calledWithExactly('token', [
'target-123',
'target-321',
]),
);
// Verify the query eventually succeeded
assert.strictEqual(results.length, 5);
// Verify query was called twice (initial fail + retry)
assert.ok(applicationAdapter.query.calledTwice);
});
});