Skip to content

Commit

Permalink
Some tweaks to favorites (#2563)
Browse files Browse the repository at this point in the history
This is follow up PR to #2550,
which addresses the issues that came up from reviews.
  • Loading branch information
sjaanus committed Nov 30, 2022
1 parent 9ac6b94 commit 0d58371
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 38 deletions.
16 changes: 9 additions & 7 deletions src/lib/db/feature-strategy-store.ts
Expand Up @@ -259,14 +259,16 @@ class FeatureStrategiesStore implements IFeatureStrategiesStore {

let selectColumns = ['features_view.*'];
if (userId && this.flagResolver.isEnabled('favorites')) {
query = query.leftJoin(`favorite_features as ff`, function () {
this.on('ff.feature', 'features_view.name').andOnVal(
'ff.user_id',
'=',
userId,
);
query = query.leftJoin(`favorite_features`, function () {
this.on(
'favorite_features.feature',
'features_view.name',
).andOnVal('favorite_features.user_id', '=', userId);
});
selectColumns = [...selectColumns, 'ff.feature as favorite'];
selectColumns = [
...selectColumns,
'favorite_features.feature as favorite',
];
}
const rows = await query.select(selectColumns);
stopTimer();
Expand Down
55 changes: 24 additions & 31 deletions src/test/e2e/api/admin/favorites.e2e.test.ts
Expand Up @@ -20,8 +20,6 @@ const createFeature = async (featureName: string) => {
})
.set('Content-Type', 'application/json')
.expect(201);

// await projectService.addEnvironmentToProject('default', environment);
};

const loginRegularUser = () =>
Expand All @@ -39,6 +37,20 @@ const createUserEditorAccess = async (name, email) => {
return user;
};

const favoriteFeature = async (featureName: string) => {
await app.request
.post(`/api/admin/projects/default/features/${featureName}/favorites`)
.set('Content-Type', 'application/json')
.expect(200);
};

const unfavoriteFeature = async (featureName: string) => {
await app.request
.delete(`/api/admin/projects/default/features/${featureName}/favorites`)
.set('Content-Type', 'application/json')
.expect(200);
};

beforeAll(async () => {
db = await dbInit('favorites_api_serial', getLogger);
app = await setupAppWithAuth(db.stores);
Expand Down Expand Up @@ -67,14 +79,10 @@ beforeEach(async () => {
await loginRegularUser();
});

test('should have favorites true in project endpoint', async () => {
test('should be favorited in project endpoint', async () => {
const featureName = 'test-feature';
await createFeature(featureName);

await app.request
.post(`/api/admin/projects/default/features/${featureName}/favorites`)
.set('Content-Type', 'application/json')
.expect(200);
await favoriteFeature(featureName);

const { body } = await app.request
.get(`/api/admin/projects/default/features`)
Expand All @@ -88,7 +96,7 @@ test('should have favorites true in project endpoint', async () => {
});
});

test('should have favorites false by default', async () => {
test('feature should not be favorited by default', async () => {
const featureName = 'test-feature';
await createFeature(featureName);

Expand All @@ -104,14 +112,10 @@ test('should have favorites false by default', async () => {
});
});

test('should have favorites true in admin endpoint', async () => {
test('should be favorited in admin endpoint', async () => {
const featureName = 'test-feature';
await createFeature(featureName);

await app.request
.post(`/api/admin/projects/default/features/${featureName}/favorites`)
.set('Content-Type', 'application/json')
.expect(200);
await favoriteFeature(featureName);

const { body } = await app.request
.get(`/api/admin/features`)
Expand All @@ -125,14 +129,10 @@ test('should have favorites true in admin endpoint', async () => {
});
});

test('should have favorites true in project single feature endpoint', async () => {
test('should be favorited in project single feature endpoint', async () => {
const featureName = 'test-feature';
await createFeature(featureName);

await app.request
.post(`/api/admin/projects/default/features/${featureName}/favorites`)
.set('Content-Type', 'application/json')
.expect(200);
await favoriteFeature(featureName);

const { body } = await app.request
.get(`/api/admin/projects/default/features/${featureName}`)
Expand All @@ -145,19 +145,12 @@ test('should have favorites true in project single feature endpoint', async () =
});
});

test('should have favorites false after deleting favorite', async () => {
test('should be able to unfavorite feature', async () => {
const featureName = 'test-feature';
await createFeature(featureName);

await app.request
.post(`/api/admin/projects/default/features/${featureName}/favorites`)
.set('Content-Type', 'application/json')
.expect(200);

await app.request
.delete(`/api/admin/projects/default/features/${featureName}/favorites`)
.set('Content-Type', 'application/json')
.expect(200);
await favoriteFeature(featureName);
await unfavoriteFeature(featureName);

const { body } = await app.request
.get(`/api/admin/projects/default/features/${featureName}`)
Expand Down

0 comments on commit 0d58371

Please sign in to comment.