NotesE2E: Fix e2e test

split success and fail cases in separate tests for better readability
add the correct user to all notes created by service (instead of api) to make the permissions checks viable.
extracted test content of notes to a global variable.

Signed-off-by: Philip Molares <philip.molares@udo.edu>
This commit is contained in:
Philip Molares 2021-02-17 13:20:54 +01:00 committed by Yannick Bungers
parent 1aa821460f
commit 8e9717c92a

View file

@ -20,11 +20,15 @@ import { PermissionsModule } from '../../src/permissions/permissions.module';
import { AuthModule } from '../../src/auth/auth.module'; import { AuthModule } from '../../src/auth/auth.module';
import { TokenAuthGuard } from '../../src/auth/token-auth.guard'; import { TokenAuthGuard } from '../../src/auth/token-auth.guard';
import { MockAuthGuard } from '../../src/auth/mock-auth.guard'; import { MockAuthGuard } from '../../src/auth/mock-auth.guard';
import { UsersService } from '../../src/users/users.service';
import { User } from '../../src/users/user.entity';
import { UsersModule } from '../../src/users/users.module'; import { UsersModule } from '../../src/users/users.module';
describe('Notes', () => { describe('Notes', () => {
let app: INestApplication; let app: INestApplication;
let notesService: NotesService; let notesService: NotesService;
let user: User;
let content: string;
beforeAll(async () => { beforeAll(async () => {
const moduleRef = await Test.createTestingModule({ const moduleRef = await Test.createTestingModule({
@ -56,14 +60,16 @@ describe('Notes', () => {
app = moduleRef.createNestApplication(); app = moduleRef.createNestApplication();
await app.init(); await app.init();
notesService = moduleRef.get(NotesService); notesService = moduleRef.get(NotesService);
const userService = moduleRef.get(UsersService);
user = await userService.createUser('hardcoded', 'Testy');
content = 'This is a test note.';
}); });
it(`POST /notes`, async () => { it('POST /notes', async () => {
const newNote = 'This is a test note.';
const response = await request(app.getHttpServer()) const response = await request(app.getHttpServer())
.post('/notes') .post('/notes')
.set('Content-Type', 'text/markdown') .set('Content-Type', 'text/markdown')
.send(newNote) .send(content)
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(201); .expect(201);
expect(response.body.metadata?.id).toBeDefined(); expect(response.body.metadata?.id).toBeDefined();
@ -71,31 +77,34 @@ describe('Notes', () => {
await notesService.getCurrentContent( await notesService.getCurrentContent(
await notesService.getNoteByIdOrAlias(response.body.metadata.id), await notesService.getNoteByIdOrAlias(response.body.metadata.id),
), ),
).toEqual(newNote); ).toEqual(content);
}); });
it(`GET /notes/{note}`, async () => { describe('GET /notes/{note}', () => {
it('works with an existing note', async () => {
// check if we can succefully get a note that exists // check if we can succefully get a note that exists
await notesService.createNote('This is a test note.', 'test1'); await notesService.createNote(content, 'test1', user);
const response = await request(app.getHttpServer()) const response = await request(app.getHttpServer())
.get('/notes/test1') .get('/notes/test1')
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200); .expect(200);
expect(response.body.content).toEqual('This is a test note.'); expect(response.body.content).toEqual(content);
});
it('fails with an non-existing note', async () => {
// check if a missing note correctly returns 404 // check if a missing note correctly returns 404
await request(app.getHttpServer()) await request(app.getHttpServer())
.get('/notes/i_dont_exist') .get('/notes/i_dont_exist')
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(404); .expect(404);
}); });
});
it(`POST /notes/{note}`, async () => { describe('POST /notes/{note}', () => {
const newNote = 'This is a test note.'; it('works with a non-existing alias', async () => {
const response = await request(app.getHttpServer()) const response = await request(app.getHttpServer())
.post('/notes/test2') .post('/notes/test2')
.set('Content-Type', 'text/markdown') .set('Content-Type', 'text/markdown')
.send(newNote) .send(content)
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(201); .expect(201);
expect(response.body.metadata?.id).toBeDefined(); expect(response.body.metadata?.id).toBeDefined();
@ -103,56 +112,63 @@ describe('Notes', () => {
await notesService.getCurrentContent( await notesService.getCurrentContent(
await notesService.getNoteByIdOrAlias(response.body.metadata?.id), await notesService.getNoteByIdOrAlias(response.body.metadata?.id),
), ),
).toEqual(newNote); ).toEqual(content);
});
}); });
it(`DELETE /notes/{note}`, async () => { describe('DELETE /notes/{note}', () => {
await notesService.createNote('This is a test note.', 'test3'); it('works with an existing alias', async () => {
await notesService.createNote(content, 'test3', user);
await request(app.getHttpServer()).delete('/notes/test3').expect(200); await request(app.getHttpServer()).delete('/notes/test3').expect(200);
await expect(notesService.getNoteByIdOrAlias('test3')).rejects.toEqual( await expect(notesService.getNoteByIdOrAlias('test3')).rejects.toEqual(
new NotInDBError("Note with id/alias 'test3' not found."), new NotInDBError("Note with id/alias 'test3' not found."),
); );
// check if a missing note correctly returns 404 });
it('fails with a non-existing alias', async () => {
await request(app.getHttpServer()) await request(app.getHttpServer())
.delete('/notes/i_dont_exist') .delete('/notes/i_dont_exist')
.expect(404); .expect(404);
}); });
});
it(`PUT /notes/{note}`, async () => { describe('PUT /notes/{note}', () => {
await notesService.createNote('This is a test note.', 'test4'); const changedContent = 'New note text';
it('works with existing alias', async () => {
await notesService.createNote(content, 'test4', user);
const response = await request(app.getHttpServer()) const response = await request(app.getHttpServer())
.put('/notes/test4') .put('/notes/test4')
.set('Content-Type', 'text/markdown') .set('Content-Type', 'text/markdown')
.send('New note text') .send(changedContent)
.expect(200); .expect(200);
await expect( await expect(
await notesService.getCurrentContent( await notesService.getCurrentContent(
await notesService.getNoteByIdOrAlias('test4'), await notesService.getNoteByIdOrAlias('test4'),
), ),
).toEqual('New note text'); ).toEqual(changedContent);
expect(response.body.content).toEqual('New note text'); expect(response.body.content).toEqual(changedContent);
});
// check if a missing note correctly returns 404 it('fails with a non-existing alias', async () => {
await request(app.getHttpServer()) await request(app.getHttpServer())
.put('/notes/i_dont_exist') .put('/notes/i_dont_exist')
.set('Content-Type', 'text/markdown') .set('Content-Type', 'text/markdown')
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(404); .expect(404);
}); });
});
describe('GET /notes/{note}/metadata', () => { describe('GET /notes/{note}/metadata', () => {
it(`returns complete metadata object`, async () => { it('returns complete metadata object', async () => {
await notesService.createNote('This is a test note.', 'test6'); await notesService.createNote(content, 'test5', user);
const metadata = await request(app.getHttpServer()) const metadata = await request(app.getHttpServer())
.get('/notes/test6/metadata') .get('/notes/test5/metadata')
.expect(200); .expect(200);
expect(typeof metadata.body.id).toEqual('string'); expect(typeof metadata.body.id).toEqual('string');
expect(metadata.body.alias).toEqual('test6'); expect(metadata.body.alias).toEqual('test5');
expect(metadata.body.title).toBeNull(); expect(metadata.body.title).toBeNull();
expect(metadata.body.description).toBeNull(); expect(metadata.body.description).toBeNull();
expect(typeof metadata.body.createTime).toEqual('string'); expect(typeof metadata.body.createTime).toEqual('string');
expect(metadata.body.editedBy).toEqual([]); expect(metadata.body.editedBy).toEqual([]);
expect(metadata.body.permissions.owner).toBeNull(); expect(metadata.body.permissions.owner.userName).toEqual('hardcoded');
expect(metadata.body.permissions.sharedToUsers).toEqual([]); expect(metadata.body.permissions.sharedToUsers).toEqual([]);
expect(metadata.body.permissions.sharedToUsers).toEqual([]); expect(metadata.body.permissions.sharedToUsers).toEqual([]);
expect(metadata.body.tags).toEqual([]); expect(metadata.body.tags).toEqual([]);
@ -163,7 +179,9 @@ describe('Notes', () => {
expect(typeof metadata.body.updateUser.photo).toEqual('string'); expect(typeof metadata.body.updateUser.photo).toEqual('string');
expect(typeof metadata.body.viewCount).toEqual('number'); expect(typeof metadata.body.viewCount).toEqual('number');
expect(metadata.body.editedBy).toEqual([]); expect(metadata.body.editedBy).toEqual([]);
});
it('fails with non-existing alias', async () => {
// check if a missing note correctly returns 404 // check if a missing note correctly returns 404
await request(app.getHttpServer()) await request(app.getHttpServer())
.get('/notes/i_dont_exist/metadata') .get('/notes/i_dont_exist/metadata')
@ -173,67 +191,76 @@ describe('Notes', () => {
it('has the correct update/create dates', async () => { it('has the correct update/create dates', async () => {
// create a note // create a note
const note = await notesService.createNote( const note = await notesService.createNote(content, 'test5a', user);
'This is a test note.',
'test6a',
);
// save the creation time // save the creation time
const createDate = (await note.revisions)[0].createdAt; const createDate = (await note.revisions)[0].createdAt;
// wait one second // wait one second
await new Promise((r) => setTimeout(r, 1000)); await new Promise((r) => setTimeout(r, 1000));
// update the note // update the note
await notesService.updateNoteByIdOrAlias('test6a', 'More test content'); await notesService.updateNoteByIdOrAlias('test5a', 'More test content');
const metadata = await request(app.getHttpServer()) const metadata = await request(app.getHttpServer())
.get('/notes/test6a/metadata') .get('/notes/test5a/metadata')
.expect(200); .expect(200);
expect(metadata.body.createTime).toEqual(createDate.toISOString()); expect(metadata.body.createTime).toEqual(createDate.toISOString());
expect(metadata.body.updateTime).not.toEqual(createDate.toISOString()); expect(metadata.body.updateTime).not.toEqual(createDate.toISOString());
}); });
}); });
it(`GET /notes/{note}/revisions`, async () => { describe('GET /notes/{note}/revisions', () => {
await notesService.createNote('This is a test note.', 'test7'); it('works with existing alias', async () => {
await notesService.createNote(content, 'test6', user);
const response = await request(app.getHttpServer()) const response = await request(app.getHttpServer())
.get('/notes/test7/revisions') .get('/notes/test6/revisions')
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200); .expect(200);
expect(response.body).toHaveLength(1); expect(response.body).toHaveLength(1);
});
it('fails with non-existing alias', async () => {
// check if a missing note correctly returns 404 // check if a missing note correctly returns 404
await request(app.getHttpServer()) await request(app.getHttpServer())
.get('/notes/i_dont_exist/revisions') .get('/notes/i_dont_exist/revisions')
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(404); .expect(404);
}); });
});
it(`GET /notes/{note}/revisions/{revision-id}`, async () => { describe('GET /notes/{note}/revisions/{revision-id}', () => {
const note = await notesService.createNote('This is a test note.', 'test8'); it('works with an existing alias', async () => {
const note = await notesService.createNote(content, 'test7', user);
const revision = await notesService.getLatestRevision(note); const revision = await notesService.getLatestRevision(note);
const response = await request(app.getHttpServer()) const response = await request(app.getHttpServer())
.get('/notes/test8/revisions/' + revision.id) .get('/notes/test7/revisions/' + revision.id)
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200); .expect(200);
expect(response.body.content).toEqual('This is a test note.'); expect(response.body.content).toEqual(content);
});
it('fails with non-existing alias', async () => {
// check if a missing note correctly returns 404 // check if a missing note correctly returns 404
await request(app.getHttpServer()) await request(app.getHttpServer())
.get('/notes/i_dont_exist/revisions/1') .get('/notes/i_dont_exist/revisions/1')
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(404); .expect(404);
}); });
});
it(`GET /notes/{note}/content`, async () => { describe('GET /notes/{note}/content', () => {
await notesService.createNote('This is a test note.', 'test9'); it('works with an existing alias', async () => {
await notesService.createNote(content, 'test8', user);
const response = await request(app.getHttpServer()) const response = await request(app.getHttpServer())
.get('/notes/test9/content') .get('/notes/test8/content')
.expect(200); .expect(200);
expect(response.text).toEqual('This is a test note.'); expect(response.text).toEqual(content);
});
it('fails with non-existing alias', async () => {
// check if a missing note correctly returns 404 // check if a missing note correctly returns 404
await request(app.getHttpServer()) await request(app.getHttpServer())
.get('/notes/i_dont_exist/content') .get('/notes/i_dont_exist/content')
.expect('Content-Type', /text\/markdown/)
.expect(404); .expect(404);
}); });
});
afterAll(async () => { afterAll(async () => {
await app.close(); await app.close();