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 David Mehren
parent 3953f6893b
commit bb04da46be
No known key found for this signature in database
GPG key ID: 185982BA4C42B7C3

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,88 +77,98 @@ 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}', () => {
// check if we can succefully get a note that exists it('works with an existing note', async () => {
await notesService.createNote('This is a test note.', 'test1'); // check if we can succefully get a note that exists
const response = await request(app.getHttpServer()) await notesService.createNote(content, 'test1', user);
.get('/notes/test1') const response = await request(app.getHttpServer())
.expect('Content-Type', /json/) .get('/notes/test1')
.expect(200); .expect('Content-Type', /json/)
expect(response.body.content).toEqual('This is a test note.'); .expect(200);
expect(response.body.content).toEqual(content);
// check if a missing note correctly returns 404 });
await request(app.getHttpServer()) it('fails with an non-existing note', async () => {
.get('/notes/i_dont_exist') // check if a missing note correctly returns 404
.expect('Content-Type', /json/) await request(app.getHttpServer())
.expect(404); .get('/notes/i_dont_exist')
.expect('Content-Type', /json/)
.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();
return expect( return expect(
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 request(app.getHttpServer()).delete('/notes/test3').expect(200); await notesService.createNote(content, 'test3', user);
await expect(notesService.getNoteByIdOrAlias('test3')).rejects.toEqual( await request(app.getHttpServer()).delete('/notes/test3').expect(200);
new NotInDBError("Note with id/alias 'test3' not found."), await expect(notesService.getNoteByIdOrAlias('test3')).rejects.toEqual(
); new NotInDBError("Note with id/alias 'test3' not found."),
// check if a missing note correctly returns 404 );
await request(app.getHttpServer()) });
.delete('/notes/i_dont_exist') it('fails with a non-existing alias', async () => {
.expect(404); await request(app.getHttpServer())
.delete('/notes/i_dont_exist')
.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';
const response = await request(app.getHttpServer()) it('works with existing alias', async () => {
.put('/notes/test4') await notesService.createNote(content, 'test4', user);
.set('Content-Type', 'text/markdown') const response = await request(app.getHttpServer())
.send('New note text') .put('/notes/test4')
.expect(200); .set('Content-Type', 'text/markdown')
await expect( .send(changedContent)
await notesService.getCurrentContent( .expect(200);
await notesService.getNoteByIdOrAlias('test4'), await expect(
), await notesService.getCurrentContent(
).toEqual('New note text'); await notesService.getNoteByIdOrAlias('test4'),
expect(response.body.content).toEqual('New note text'); ),
).toEqual(changedContent);
// check if a missing note correctly returns 404 expect(response.body.content).toEqual(changedContent);
await request(app.getHttpServer()) });
.put('/notes/i_dont_exist') it('fails with a non-existing alias', async () => {
.set('Content-Type', 'text/markdown') await request(app.getHttpServer())
.expect('Content-Type', /json/) .put('/notes/i_dont_exist')
.expect(404); .set('Content-Type', 'text/markdown')
.expect('Content-Type', /json/)
.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,66 +191,75 @@ 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 () => {
const response = await request(app.getHttpServer()) await notesService.createNote(content, 'test6', user);
.get('/notes/test7/revisions') const response = await request(app.getHttpServer())
.expect('Content-Type', /json/) .get('/notes/test6/revisions')
.expect(200); .expect('Content-Type', /json/)
expect(response.body).toHaveLength(1); .expect(200);
expect(response.body).toHaveLength(1);
});
// check if a missing note correctly returns 404 it('fails with non-existing alias', async () => {
await request(app.getHttpServer()) // check if a missing note correctly returns 404
.get('/notes/i_dont_exist/revisions') await request(app.getHttpServer())
.expect('Content-Type', /json/) .get('/notes/i_dont_exist/revisions')
.expect(404); .expect('Content-Type', /json/)
.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 revision = await notesService.getLatestRevision(note); const note = await notesService.createNote(content, 'test7', user);
const response = await request(app.getHttpServer()) const revision = await notesService.getLatestRevision(note);
.get('/notes/test8/revisions/' + revision.id) const response = await request(app.getHttpServer())
.expect('Content-Type', /json/) .get('/notes/test7/revisions/' + revision.id)
.expect(200); .expect('Content-Type', /json/)
expect(response.body.content).toEqual('This is a test note.'); .expect(200);
expect(response.body.content).toEqual(content);
// check if a missing note correctly returns 404 });
await request(app.getHttpServer()) it('fails with non-existing alias', async () => {
.get('/notes/i_dont_exist/revisions/1') // check if a missing note correctly returns 404
.expect('Content-Type', /json/) await request(app.getHttpServer())
.expect(404); .get('/notes/i_dont_exist/revisions/1')
.expect('Content-Type', /json/)
.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 () => {
const response = await request(app.getHttpServer()) await notesService.createNote(content, 'test8', user);
.get('/notes/test9/content') const response = await request(app.getHttpServer())
.expect(200); .get('/notes/test8/content')
expect(response.text).toEqual('This is a test note.'); .expect(200);
expect(response.text).toEqual(content);
});
// check if a missing note correctly returns 404 it('fails with non-existing alias', async () => {
await request(app.getHttpServer()) // check if a missing note correctly returns 404
.get('/notes/i_dont_exist/content') await request(app.getHttpServer())
.expect(404); .get('/notes/i_dont_exist/content')
.expect('Content-Type', /text\/markdown/)
.expect(404);
});
}); });
afterAll(async () => { afterAll(async () => {