mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-15 05:37:32 +00:00
test(server): all instance variants (#7882)
This commit is contained in:
168
packages/backend/server/tests/app/selfhost.e2e.ts
Normal file
168
packages/backend/server/tests/app/selfhost.e2e.ts
Normal file
@@ -0,0 +1,168 @@
|
||||
import { mkdirSync, writeFileSync } from 'node:fs';
|
||||
import path from 'node:path';
|
||||
|
||||
import type { INestApplication } from '@nestjs/common';
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
import type { TestFn } from 'ava';
|
||||
import ava from 'ava';
|
||||
import request from 'supertest';
|
||||
|
||||
import { buildAppModule } from '../../src/app.module';
|
||||
import { ServerService } from '../../src/core/config';
|
||||
import { Config } from '../../src/fundamentals';
|
||||
import { createTestingApp, initTestingDB } from '../utils';
|
||||
|
||||
const test = ava as TestFn<{
|
||||
app: INestApplication;
|
||||
db: PrismaClient;
|
||||
}>;
|
||||
|
||||
function initTestStaticFiles(staticPath: string) {
|
||||
mkdirSync(path.join(staticPath, 'admin'), { recursive: true });
|
||||
const files = {
|
||||
'index.html': `<!DOCTYPE html><html><body>AFFiNE</body><script src="main.js"/></html>`,
|
||||
'main.js': `const name = 'affine'`,
|
||||
'admin/index.html': `<!DOCTYPE html><html><body>AFFiNE Admin</body><script src="/admin/main.js"/></html>`,
|
||||
'admin/main.js': `const name = 'affine-admin'`,
|
||||
};
|
||||
|
||||
for (const [filename, content] of Object.entries(files)) {
|
||||
writeFileSync(path.join(staticPath, filename), content);
|
||||
}
|
||||
}
|
||||
|
||||
test.before('init selfhost server', async t => {
|
||||
// @ts-expect-error override
|
||||
AFFiNE.isSelfhosted = true;
|
||||
const { app } = await createTestingApp({
|
||||
imports: [buildAppModule()],
|
||||
});
|
||||
|
||||
t.context.app = app;
|
||||
t.context.db = t.context.app.get(PrismaClient);
|
||||
const config = app.get(Config);
|
||||
|
||||
const staticPath = path.join(config.projectRoot, 'static');
|
||||
initTestStaticFiles(staticPath);
|
||||
});
|
||||
|
||||
test.beforeEach(async t => {
|
||||
await initTestingDB(t.context.db);
|
||||
const server = t.context.app.get(ServerService);
|
||||
// @ts-expect-error disable cache
|
||||
server._initialized = false;
|
||||
});
|
||||
|
||||
test.afterEach.always(async t => {
|
||||
await t.context.app.close();
|
||||
});
|
||||
|
||||
test('do not allow visit index.html directly', async t => {
|
||||
let res = await request(t.context.app.getHttpServer())
|
||||
.get('/index.html')
|
||||
.expect(302);
|
||||
|
||||
t.is(res.header.location, '');
|
||||
|
||||
res = await request(t.context.app.getHttpServer())
|
||||
.get('/admin/index.html')
|
||||
.expect(302);
|
||||
|
||||
t.is(res.header.location, '/admin');
|
||||
});
|
||||
|
||||
test('should always return static asset files', async t => {
|
||||
let res = await request(t.context.app.getHttpServer())
|
||||
.get('/main.js')
|
||||
.expect(200);
|
||||
t.is(res.text, "const name = 'affine'");
|
||||
|
||||
res = await request(t.context.app.getHttpServer())
|
||||
.get('/admin/main.js')
|
||||
.expect(200);
|
||||
t.is(res.text, "const name = 'affine-admin'");
|
||||
|
||||
await t.context.db.user.create({
|
||||
data: {
|
||||
name: 'test',
|
||||
email: 'test@affine.pro',
|
||||
},
|
||||
});
|
||||
|
||||
res = await request(t.context.app.getHttpServer())
|
||||
.get('/main.js')
|
||||
.expect(200);
|
||||
t.is(res.text, "const name = 'affine'");
|
||||
|
||||
res = await request(t.context.app.getHttpServer())
|
||||
.get('/admin/main.js')
|
||||
.expect(200);
|
||||
t.is(res.text, "const name = 'affine-admin'");
|
||||
});
|
||||
|
||||
test('should be able to call apis', async t => {
|
||||
const res = await request(t.context.app.getHttpServer())
|
||||
.get('/info')
|
||||
.expect(200);
|
||||
|
||||
t.is(res.body.flavor, 'allinone');
|
||||
});
|
||||
|
||||
const blockedPages = [
|
||||
'/',
|
||||
'/workspace',
|
||||
'/admin',
|
||||
'/admin/',
|
||||
'/admin/accounts',
|
||||
];
|
||||
|
||||
test('should redirect to setup if server is not initialized', async t => {
|
||||
for (const path of blockedPages) {
|
||||
const res = await request(t.context.app.getHttpServer()).get(path);
|
||||
|
||||
t.is(res.status, 302, `Failed to redirect ${path}`);
|
||||
t.is(res.header.location, '/admin/setup');
|
||||
}
|
||||
|
||||
t.pass();
|
||||
});
|
||||
|
||||
test('should allow visiting all pages if initialized', async t => {
|
||||
await t.context.db.user.create({
|
||||
data: {
|
||||
name: 'test',
|
||||
email: 'test@affine.pro',
|
||||
},
|
||||
});
|
||||
|
||||
for (const path of blockedPages) {
|
||||
const res = await request(t.context.app.getHttpServer()).get(path);
|
||||
|
||||
t.is(res.status, 200, `Failed to visit ${path}`);
|
||||
}
|
||||
|
||||
t.pass();
|
||||
});
|
||||
|
||||
test('should allow visiting setup page if not initialized', async t => {
|
||||
const res = await request(t.context.app.getHttpServer())
|
||||
.get('/admin/setup')
|
||||
.expect(200);
|
||||
|
||||
t.true(res.text.includes('AFFiNE Admin'));
|
||||
});
|
||||
|
||||
test('should redirect to admin if initialized', async t => {
|
||||
await t.context.db.user.create({
|
||||
data: {
|
||||
name: 'test',
|
||||
email: 'test@affine.pro',
|
||||
},
|
||||
});
|
||||
|
||||
const res = await request(t.context.app.getHttpServer())
|
||||
.get('/admin/setup')
|
||||
.expect(302);
|
||||
|
||||
t.is(res.header.location, '/admin');
|
||||
});
|
||||
Reference in New Issue
Block a user