feat: user usage gql & test case improve (#5076)

This commit is contained in:
DarkSky
2023-12-14 09:50:41 +00:00
parent ad23ead5e4
commit a93c12e122
22 changed files with 364 additions and 132 deletions

View File

@@ -126,7 +126,6 @@ test('should find default user', async t => {
})
.expect(200)
.expect(res => {
console.log(res.body);
t.is(res.body.data.user.email, 'alex.yang@example.org');
});
});

View File

@@ -9,16 +9,13 @@ import ava, { type TestFn } from 'ava';
import graphqlUploadExpress from 'graphql-upload/graphqlUploadExpress.mjs';
import { AppModule } from '../src/app';
import {
collectMigrations,
RevertCommand,
RunCommand,
} from '../src/data/commands/run';
import { RevertCommand, RunCommand } from '../src/data/commands/run';
import { MailService } from '../src/modules/auth/mailer';
import { AuthService } from '../src/modules/auth/service';
import {
changeEmail,
createWorkspace,
initFeatureConfigs,
sendChangeEmail,
sendVerifyChangeEmail,
signUp,
@@ -60,11 +57,7 @@ test.beforeEach(async t => {
t.context.mail = mail;
// init features
const run = module.get(RunCommand);
const revert = module.get(RevertCommand);
const migrations = await collectMigrations();
await Promise.allSettled(migrations.map(m => revert.run([m.name])));
await run.run();
await initFeatureConfigs(module);
});
test.afterEach(async t => {

View File

@@ -4,11 +4,7 @@ import { PrismaClient } from '@prisma/client';
import test from 'ava';
import { ConfigModule } from '../src/config';
import {
collectMigrations,
RevertCommand,
RunCommand,
} from '../src/data/commands/run';
import { RevertCommand, RunCommand } from '../src/data/commands/run';
import { GqlModule } from '../src/graphql.module';
import { AuthModule } from '../src/modules/auth';
import { AuthResolver } from '../src/modules/auth/resolver';
@@ -16,6 +12,7 @@ import { AuthService } from '../src/modules/auth/service';
import { PrismaModule } from '../src/prisma';
import { mintChallengeResponse, verifyChallengeResponse } from '../src/storage';
import { RateLimiterModule } from '../src/throttler';
import { initFeatureConfigs } from './utils';
let authService: AuthService;
let authResolver: AuthResolver;
@@ -53,11 +50,7 @@ test.beforeEach(async () => {
authResolver = module.get(AuthResolver);
// init features
const run = module.get(RunCommand);
const revert = module.get(RevertCommand);
const migrations = await collectMigrations();
await Promise.allSettled(migrations.map(m => revert.run([m.name])));
await run.run();
await initFeatureConfigs(module);
});
test.afterEach.always(async () => {

View File

@@ -0,0 +1,140 @@
/// <reference types="../src/global.d.ts" />
import { Test, TestingModule } from '@nestjs/testing';
import { PrismaClient } from '@prisma/client';
import ava, { type TestFn } from 'ava';
import { ConfigModule } from '../src/config';
import { RevertCommand, RunCommand } from '../src/data/commands/run';
import { AuthModule } from '../src/modules/auth';
import { AuthService } from '../src/modules/auth/service';
import {
FeatureManagementService,
FeatureModule,
FeatureService,
FeatureType,
} from '../src/modules/features';
import { PrismaModule } from '../src/prisma';
import { RateLimiterModule } from '../src/throttler';
import { initFeatureConfigs } from './utils';
const test = ava as TestFn<{
auth: AuthService;
feature: FeatureService;
early_access: FeatureManagementService;
app: TestingModule;
}>;
// cleanup database before each test
test.beforeEach(async () => {
const client = new PrismaClient();
await client.$connect();
await client.user.deleteMany({});
await client.$disconnect();
});
test.beforeEach(async t => {
const module = await Test.createTestingModule({
imports: [
ConfigModule.forRoot({
auth: {
accessTokenExpiresIn: 1,
refreshTokenExpiresIn: 1,
leeway: 1,
},
host: 'example.org',
https: true,
featureFlags: {
earlyAccessPreview: true,
},
}),
PrismaModule,
AuthModule,
FeatureModule,
RateLimiterModule,
RevertCommand,
RunCommand,
],
}).compile();
const quota = module.get(FeatureService);
const storageQuota = module.get(FeatureManagementService);
const auth = module.get(AuthService);
t.context.app = module;
t.context.feature = quota;
t.context.early_access = storageQuota;
t.context.auth = auth;
// init features
await initFeatureConfigs(module);
});
test.afterEach.always(async t => {
await t.context.app.close();
});
test('should be able to set feature', async t => {
const { auth, feature } = t.context;
const u1 = await auth.signUp('DarkSky', 'darksky@example.org', '123456');
const f1 = await feature.getUserFeatures(u1.id);
t.is(f1.length, 0, 'should be empty');
await feature.addUserFeature(u1.id, FeatureType.EarlyAccess, 1, 'test');
const f2 = await feature.getUserFeatures(u1.id);
t.is(f2.length, 1, 'should have one feature');
t.is(
f2[0].feature.feature,
FeatureType.EarlyAccess,
'should be early access'
);
});
test('should be able to check early access', async t => {
const { auth, feature, early_access } = t.context;
const u1 = await auth.signUp('DarkSky', 'darksky@example.org', '123456');
const f1 = await early_access.canEarlyAccess(u1.email);
t.false(f1, 'should not have early access');
await early_access.addEarlyAccess(u1.id);
const f2 = await early_access.canEarlyAccess(u1.email);
t.true(f2, 'should have early access');
const f3 = await feature.listFeatureUsers(FeatureType.EarlyAccess);
t.is(f3.length, 1, 'should have one user');
t.is(f3[0].id, u1.id, 'should be the same user');
});
test('should be able revert quota', async t => {
const { auth, feature, early_access } = t.context;
const u1 = await auth.signUp('DarkSky', 'darksky@example.org', '123456');
const f1 = await early_access.canEarlyAccess(u1.email);
t.false(f1, 'should not have early access');
await early_access.addEarlyAccess(u1.id);
const f2 = await early_access.canEarlyAccess(u1.email);
t.true(f2, 'should have early access');
const q1 = await early_access.listEarlyAccess();
t.is(q1.length, 1, 'should have one user');
t.is(q1[0].id, u1.id, 'should be the same user');
await early_access.removeEarlyAccess(u1.id);
const f3 = await early_access.canEarlyAccess(u1.email);
t.false(f3, 'should not have early access');
const q2 = await early_access.listEarlyAccess();
t.is(q2.length, 0, 'should have no user');
const q3 = await feature.getUserFeatures(u1.id);
t.is(q3.length, 1, 'should have 1 feature');
t.is(
q3[0].feature.feature,
FeatureType.EarlyAccess,
'should be early access'
);
t.is(q3[0].activated, false, 'should be deactivated');
});

View File

@@ -11,16 +11,13 @@ import { PrismaClient } from '@prisma/client';
import ava, { type TestFn } from 'ava';
import { ConfigModule } from '../src/config';
import {
collectMigrations,
RevertCommand,
RunCommand,
} from '../src/data/commands/run';
import { RevertCommand, RunCommand } from '../src/data/commands/run';
import { GqlModule } from '../src/graphql.module';
import { AuthModule } from '../src/modules/auth';
import { AuthService } from '../src/modules/auth/service';
import { PrismaModule } from '../src/prisma';
import { RateLimiterModule } from '../src/throttler';
import { initFeatureConfigs } from './utils';
const test = ava as TestFn<{
auth: AuthService;
@@ -55,11 +52,7 @@ test.beforeEach(async t => {
t.context.auth = t.context.module.get(AuthService);
// init features
const run = t.context.module.get(RunCommand);
const revert = t.context.module.get(RevertCommand);
const migrations = await collectMigrations();
await Promise.allSettled(migrations.map(m => revert.run([m.name])));
await run.run();
await initFeatureConfigs(t.context.module);
});
test.afterEach.always(async t => {

View File

@@ -5,11 +5,7 @@ import { PrismaClient } from '@prisma/client';
import ava, { type TestFn } from 'ava';
import { ConfigModule } from '../src/config';
import {
collectMigrations,
RevertCommand,
RunCommand,
} from '../src/data/commands/run';
import { RevertCommand, RunCommand } from '../src/data/commands/run';
import { AuthModule } from '../src/modules/auth';
import { AuthService } from '../src/modules/auth/service';
import {
@@ -22,6 +18,7 @@ import {
import { PrismaModule } from '../src/prisma';
import { StorageModule } from '../src/storage';
import { RateLimiterModule } from '../src/throttler';
import { initFeatureConfigs } from './utils';
const test = ava as TestFn<{
auth: AuthService;
@@ -70,11 +67,7 @@ test.beforeEach(async t => {
t.context.auth = auth;
// init features
const run = module.get(RunCommand);
const revert = module.get(RevertCommand);
const migrations = await collectMigrations();
await Promise.allSettled(migrations.map(m => revert.run([m.name])));
await run.run();
await initFeatureConfigs(module);
});
test.afterEach.always(async t => {

View File

@@ -1,10 +1,12 @@
import { randomUUID } from 'node:crypto';
import type { INestApplication } from '@nestjs/common';
import { TestingModule } from '@nestjs/testing';
import { hashSync } from '@node-rs/argon2';
import { PrismaClient, type User } from '@prisma/client';
import request from 'supertest';
import { RevertCommand, RunCommand } from '../src/data/commands/run';
import type { TokenType } from '../src/modules/auth';
import type { UserType } from '../src/modules/users';
import type { InvitationType, WorkspaceType } from '../src/modules/workspaces';
@@ -561,6 +563,13 @@ export class FakePrisma {
}
}
export async function initFeatureConfigs(module: TestingModule) {
const run = module.get(RunCommand);
const revert = module.get(RevertCommand);
await Promise.allSettled([revert.run(['UserFeaturesInit1698652531198'])]);
await run.runOne('UserFeaturesInit1698652531198');
}
export {
acceptInviteById,
changeEmail,

View File

@@ -6,17 +6,14 @@ import graphqlUploadExpress from 'graphql-upload/graphqlUploadExpress.mjs';
import request from 'supertest';
import { AppModule } from '../src/app';
import {
collectMigrations,
RevertCommand,
RunCommand,
} from '../src/data/commands/run';
import { RevertCommand, RunCommand } from '../src/data/commands/run';
import { QuotaService, QuotaType } from '../src/modules/quota';
import {
checkBlobSize,
collectAllBlobSizes,
createWorkspace,
getWorkspaceBlobsSize,
initFeatureConfigs,
listBlobs,
setBlob,
signUp,
@@ -52,11 +49,7 @@ test.beforeEach(async () => {
quota = module.get(QuotaService);
// init features
const run = module.get(RunCommand);
const revert = module.get(RevertCommand);
const migrations = await collectMigrations();
await Promise.allSettled(migrations.map(m => revert.run([m.name])));
await run.run();
await initFeatureConfigs(module);
await app.init();
});

View File

@@ -9,17 +9,14 @@ import ava, { type TestFn } from 'ava';
import graphqlUploadExpress from 'graphql-upload/graphqlUploadExpress.mjs';
import { AppModule } from '../src/app';
import {
collectMigrations,
RevertCommand,
RunCommand,
} from '../src/data/commands/run';
import { RevertCommand, RunCommand } from '../src/data/commands/run';
import { MailService } from '../src/modules/auth/mailer';
import { AuthService } from '../src/modules/auth/service';
import {
acceptInviteById,
createWorkspace,
getWorkspace,
initFeatureConfigs,
inviteUser,
leaveWorkspace,
revokeUser,
@@ -63,11 +60,7 @@ test.beforeEach(async t => {
t.context.mail = mail;
// init features
const run = module.get(RunCommand);
const revert = module.get(RevertCommand);
const migrations = await collectMigrations();
await Promise.allSettled(migrations.map(m => revert.run([m.name])));
await run.run();
await initFeatureConfigs(module);
});
test.afterEach.always(async t => {

View File

@@ -6,17 +6,14 @@ import graphqlUploadExpress from 'graphql-upload/graphqlUploadExpress.mjs';
import request from 'supertest';
import { AppModule } from '../src/app';
import {
collectMigrations,
RevertCommand,
RunCommand,
} from '../src/data/commands/run';
import { RevertCommand, RunCommand } from '../src/data/commands/run';
import {
acceptInviteById,
createWorkspace,
currentUser,
getPublicWorkspace,
getWorkspacePublicPages,
initFeatureConfigs,
inviteUser,
publishPage,
revokePublicPage,
@@ -53,11 +50,7 @@ test.beforeEach(async t => {
t.context.app = app;
// init features
const run = module.get(RunCommand);
const revert = module.get(RevertCommand);
const migrations = await collectMigrations();
await Promise.allSettled(migrations.map(m => revert.run([m.name])));
await run.run();
await initFeatureConfigs(module);
});
test.afterEach.always(async t => {