Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Thr 18 dev feature h5p editor #4525

Merged
merged 181 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from 177 commits
Commits
Show all changes
181 commits
Select commit Hold shift + click to select a range
d67be9f
THR-1 h5p editor: temporary file storage (#4147)
ssmid Jun 7, 2023
c2c78db
THR-2 local content storage (#4140)
SteKrause Jun 7, 2023
6709414
THR-3 local library storage (#4133)
marode-cap Jun 7, 2023
bc2e26f
h5p editor: added deployment which uses existing template (#4136)
MajedAlaitwniCap Jun 7, 2023
1ef5891
Thr 5 h5p implementation endpoints (#4169)
SteKrause Jun 7, 2023
97c63cd
Merge remote-tracking branch 'origin/main' into THR-18-dev-feature-h5…
SteKrause Jun 7, 2023
c0935db
THR-7 ContentStorage: store data on S3 via S3ClientAdapter (#4198)
SteKrause Jul 13, 2023
4befc25
Merge remote-tracking branch 'origin/main' into THR-18-dev-feature-h5…
marode-cap Jul 28, 2023
f3aba94
Thr 20 create UI elements (#4190)
marode-cap Jul 31, 2023
f3c41b1
h5p editor: library storage s3 implementation
ssmid Jul 12, 2023
b7aa03d
h5p editor: library storage updated; unit tests; bugfixes
ssmid Jul 18, 2023
d7eac9d
change to correct Buckets / Key names
MajedAlaitwniCap Aug 1, 2023
82e2652
add config keys
MajedAlaitwniCap Aug 2, 2023
aa46e74
change default value for env var S3_REGION
SteKrause Aug 3, 2023
5f8d6e8
Fix API Validation
marode-cap Aug 8, 2023
752816a
THR-25 S3 implementation of temporary file storage (#4204)
ssmid Aug 11, 2023
f9c5914
Thr 27 h5p language parameter (#4283)
SteKrause Aug 15, 2023
840a75f
THR-42 fixes to library storage (#4332)
marode-cap Aug 18, 2023
cfdee9f
Merge remote-tracking branch 'origin/main' into THR-18-dev-feature-h5…
marode-cap Aug 21, 2023
54bc0f8
Merge remote-tracking branch 'origin/main' into THR-18-dev-feature-h5…
marode-cap Aug 21, 2023
56ebbc3
delete h5p-editor static files
SteKrause Aug 21, 2023
cf7917e
Merge branch 'main' into THR-18-dev-feature-h5p-editor
SteKrause Aug 21, 2023
ab50e1d
Remove deployment from main
marode-cap Aug 21, 2023
ef3b235
remove static server
marode-cap Aug 23, 2023
add6306
add LibraryStorage to uc tests
SteKrause Aug 24, 2023
702dd35
expect LanguageType in editor uc test
SteKrause Aug 24, 2023
6023e60
add missing language param at api tests
SteKrause Aug 25, 2023
fddebc1
add field name to response type IGetFileResponse
SteKrause Aug 25, 2023
47a4a24
Merge branch 'main' into THR-18-dev-feature-h5p-editor
SteKrause Aug 25, 2023
6f0fa49
change library entity constructor
SteKrause Aug 25, 2023
d2ea67f
refactor library entity constructor
SteKrause Aug 25, 2023
8cec885
add list of files (#4349)
SevenWaysDP Aug 29, 2023
1f6da9f
code Smells corrector
MajedAlaitwniCap Aug 29, 2023
96e0aa5
Merge branch 'THR-18-dev-feature-h5p-editor' of https://github.com/hp…
MajedAlaitwniCap Aug 29, 2023
491b739
Merge branch 'main' into THR-18-dev-feature-h5p-editor
MajedAlaitwniCap Aug 29, 2023
de771ee
THR-6 H5P editor authorization (#4364)
marode-cap Aug 30, 2023
46e7055
Document custom transform pipe
marode-cap Aug 31, 2023
22cfe6e
Fix PR comments
marode-cap Aug 31, 2023
181c5a7
BaseEntityWithTimestamps for entities
marode-cap Aug 31, 2023
f81e90c
Address more PR comments
marode-cap Aug 31, 2023
85eefd7
pr comments
marode-cap Aug 31, 2023
2f1f8c7
pr comments
marode-cap Aug 31, 2023
5a39ff8
Better file handling
marode-cap Aug 31, 2023
63e4403
Missing file
marode-cap Sep 4, 2023
06cc46e
Merge remote-tracking branch 'origin/main' into THR-18-dev-feature-h5…
SevenWaysDP Sep 7, 2023
75ff0b6
fix changes from s3 module
SevenWaysDP Sep 7, 2023
c9fd20e
h5p editor: change api and static files urls
ssmid Sep 18, 2023
3f9e279
Merge branch 'main' into THR-preview
ssmid Sep 18, 2023
f0cf579
Merge branch 'main' into THR-18-dev-feature-h5p-editor
SteKrause Sep 19, 2023
07e2cd6
refactor library tests and imports
SteKrause Sep 19, 2023
e668d14
refactor imports of contentStorage and temporary file storage
SteKrause Sep 19, 2023
2926209
refactor h5p content tests
SteKrause Sep 19, 2023
72fb86b
delete unused imports
SteKrause Sep 19, 2023
aeb63ec
refactor h5p api tests
SteKrause Sep 19, 2023
a3b1b48
Merge branch 'main' into THR-18-dev-feature-h5p-editor
SteKrause Sep 20, 2023
32bbc70
refactor imports in module
SteKrause Sep 20, 2023
d86aad6
delete blank line
SteKrause Sep 20, 2023
60d5511
restructure api tests h5p-save-create
SteKrause Sep 21, 2023
0d3a031
h5p editor: temporarily dont map errors
ssmid Sep 22, 2023
3587106
refactor tests regarding authorization
SteKrause Sep 22, 2023
60c63ec
Merge branch 'main' into THR-18-dev-feature-h5p-editor
SteKrause Sep 22, 2023
ef79423
fix save-create api tests
SteKrause Sep 22, 2023
626fa3a
quick fixes service tests
SteKrause Sep 22, 2023
b6d7e46
fix test in contentStorage service
SteKrause Sep 28, 2023
0c2dcc3
refactor api editor files tests
SteKrause Sep 28, 2023
ab0c79f
Merge branch 'main' into THR-18-dev-feature-h5p-editor
SteKrause Sep 29, 2023
9442da5
Merge branch 'main' into THR-18-dev-feature-h5p-editor
casparneumann-cap Oct 5, 2023
3ca3f04
set default parameter rangeStart = 0
casparneumann-cap Oct 6, 2023
309c57d
minor code structure adjustments
casparneumann-cap Oct 6, 2023
dd41731
refactor: only one return statement
casparneumann-cap Oct 6, 2023
9363337
fix commit dd41731: redefine type of returnValue
casparneumann-cap Oct 6, 2023
7db0a03
resolve type error for response stream
casparneumann-cap Oct 6, 2023
f3d62c9
Merge branch 'main' into THR-18-dev-feature-h5p-editor
casparneumann-cap Oct 9, 2023
545e5b1
revert minor code changes
casparneumann-cap Oct 10, 2023
01c39bd
review comments controller
MajedAlaitwniCap Oct 12, 2023
6461c34
Merge branch 'main' into THR-preview
MajedAlaitwniCap Oct 12, 2023
af8b7ae
Merge branch 'main' of https://github.com/hpi-schul-cloud/schulcloud-…
MajedAlaitwniCap Oct 13, 2023
c6578f7
Merge branch 'THR-preview' of https://github.com/hpi-schul-cloud/schu…
MajedAlaitwniCap Oct 13, 2023
6f21479
Revert "review comments controller"
MajedAlaitwniCap Oct 13, 2023
0aa44a3
Merge branch 'main' of https://github.com/hpi-schul-cloud/schulcloud-…
MajedAlaitwniCap Oct 16, 2023
cb8cf58
Fix s3 client mocks
marode-cap Oct 16, 2023
d277750
Merge branch 'THR-18-dev-feature-h5p-editor' of https://github.com/hp…
MajedAlaitwniCap Oct 16, 2023
80ea9f7
Fix tests
marode-cap Oct 16, 2023
61a4018
Merge branch 'THR-18-dev-feature-h5p-editor' of github.com:hpi-schul-…
marode-cap Oct 16, 2023
87eb68d
fix file storage service test
MajedAlaitwniCap Oct 16, 2023
cbcd4e3
Fix integration tests
marode-cap Oct 17, 2023
59347d6
Merge branch 'main' into THR-18-dev-feature-h5p-editor
casparneumann-cap Oct 18, 2023
fdcb79a
add empty line before return
casparneumann-cap Oct 18, 2023
29b522e
refactor: await below ifelse block
casparneumann-cap Oct 18, 2023
cc7a537
refactor: set default for rangeStart
casparneumann-cap Oct 18, 2023
dc9ff92
Merge remote-tracking branch 'origin/main' into THR-18-dev-feature-h5…
marode-cap Oct 23, 2023
6f8b7dc
Fix imports
marode-cap Oct 23, 2023
f866fbf
Remove import from files-storage
marode-cap Oct 23, 2023
42914bb
do not overwrite input parameter
casparneumann-cap Oct 23, 2023
dab58a5
change Error Type to NotAcceptable
casparneumann-cap Oct 23, 2023
de37fa5
create own h5pfile dto
casparneumann-cap Oct 23, 2023
bd03d2c
fix remaining review comments in temporary-file-storage.service
casparneumann-cap Oct 23, 2023
c1641a9
refactor h5peditor/ h5pplayer service to provider
casparneumann-cap Oct 23, 2023
90d5496
add test for new Repo Method
MajedAlaitwniCap Oct 23, 2023
d16c225
fix unit test for Temporary-file-storage
MajedAlaitwniCap Oct 23, 2023
fdc85d3
fix test Module imports
MajedAlaitwniCap Oct 24, 2023
7f80710
fix review comments: return method and error types
casparneumann-cap Oct 24, 2023
c4181e3
create seperate h5p error mapper - unit tests still WIP
casparneumann-cap Oct 24, 2023
aa9417b
update unit tests for uc.getAjax & uc.postAjax
casparneumann-cap Oct 24, 2023
44a731f
resolve ts expect error & Test corrector
MajedAlaitwniCap Oct 24, 2023
d351edb
resolve rest review comments h5p.uc.ts
MajedAlaitwniCap Oct 24, 2023
e870724
remove unused import
MajedAlaitwniCap Oct 24, 2023
e3f16eb
move PostBodyParamsTransformPipe to extra file
MajedAlaitwniCap Oct 25, 2023
39d0c3c
rename TemporaryFile to BaseEntityWithTimestamp
MajedAlaitwniCap Oct 25, 2023
869951f
add private method and use cause error
MajedAlaitwniCap Oct 25, 2023
cce115d
refactor from Service to H5PAjaxEndpointProvider
MajedAlaitwniCap Oct 25, 2023
4bce326
use explicit return type
MajedAlaitwniCap Oct 25, 2023
36835a7
use cause error types
MajedAlaitwniCap Oct 25, 2023
71b4b0d
Merge branch 'main' of https://github.com/hpi-schul-cloud/schulcloud-…
MajedAlaitwniCap Oct 25, 2023
9d5f27e
initial commit
MajedAlaitwniCap Oct 26, 2023
3079af6
create library.repo.spec test setup and first test
casparneumann-cap Oct 26, 2023
bef74d0
create tests for library.repo
casparneumann-cap Oct 26, 2023
61337bd
revert
MajedAlaitwniCap Oct 27, 2023
940fc64
Merge branch 'THR-18-dev-feature-h5p-editor' of https://github.com/hp…
MajedAlaitwniCap Oct 27, 2023
9f66fd9
revert zu main
MajedAlaitwniCap Oct 27, 2023
aa9bd5b
last review comments
MajedAlaitwniCap Oct 27, 2023
db22208
fix library.repo.spec code coverage
casparneumann-cap Oct 27, 2023
cbae4cc
Add library.entity.spec
casparneumann-cap Oct 27, 2023
e904f19
Add test for existsOne to increase CodeCoverage
casparneumann-cap Oct 27, 2023
01ca82e
add h5p-content mapper Test
MajedAlaitwniCap Oct 30, 2023
6ad00eb
Merge branch 'THR-18-dev-feature-h5p-editor' of https://github.com/hp…
MajedAlaitwniCap Oct 30, 2023
0c23228
Merge branch 'main' into THR-18-dev-feature-h5p-editor
MajedAlaitwniCap Nov 1, 2023
9d18235
Fix eslint
MajedAlaitwniCap Nov 1, 2023
aef7727
fix Test
MajedAlaitwniCap Nov 1, 2023
2d4c71d
fix saveFile mockImplementation
casparneumann-cap Nov 1, 2023
674766e
fix unnecessary type assertion
MajedAlaitwniCap Nov 1, 2023
fcee8cb
Merge branch 'THR-18-dev-feature-h5p-editor' of https://github.com/hp…
MajedAlaitwniCap Nov 1, 2023
9b58ed7
import Correction
MajedAlaitwniCap Nov 1, 2023
f75f0b4
add tests libraryStorage.service
casparneumann-cap Nov 1, 2023
ca5e12d
remove uneccessary
casparneumann-cap Nov 1, 2023
bcffb77
Remove uneccessary import
casparneumann-cap Nov 1, 2023
24f27a5
correction and add test for h5p mapper
MajedAlaitwniCap Nov 1, 2023
49899a9
Merge branch 'THR-18-dev-feature-h5p-editor' of https://github.com/hp…
MajedAlaitwniCap Nov 1, 2023
53ca6a6
remove uneccessary imports
casparneumann-cap Nov 1, 2023
bf13f4c
Merge branch 'THR-18-dev-feature-h5p-editor' of https://github.com/hp…
casparneumann-cap Nov 1, 2023
2899bc8
remove unused import
MajedAlaitwniCap Nov 1, 2023
1b5d4fa
test structure
MajedAlaitwniCap Nov 1, 2023
6912a15
add tests for library.entity
casparneumann-cap Nov 1, 2023
6675d4c
WIP | create tests post.body.params.transform-pipe
casparneumann-cap Nov 1, 2023
664d702
add tests for post.body.params.transform-pipe
casparneumann-cap Nov 2, 2023
49f718b
remove unused parameter
MajedAlaitwniCap Nov 2, 2023
0230fc9
Merge branch 'THR-18-dev-feature-h5p-editor' of https://github.com/hp…
MajedAlaitwniCap Nov 2, 2023
f1bbb16
Create Test for H5PContentMetadata
casparneumann-cap Nov 2, 2023
2e74ac8
Merge branch 'THR-18-dev-feature-h5p-editor' of https://github.com/hp…
casparneumann-cap Nov 2, 2023
a2c55f6
refactor Method
MajedAlaitwniCap Nov 2, 2023
f7db077
Merge branch 'THR-18-dev-feature-h5p-editor' of https://github.com/hp…
MajedAlaitwniCap Nov 2, 2023
220b67c
define method return type
MajedAlaitwniCap Nov 2, 2023
829920f
Add test for temporary-file-storage.service
casparneumann-cap Nov 2, 2023
dc650d3
Merge branch 'THR-18-dev-feature-h5p-editor' of https://github.com/hp…
casparneumann-cap Nov 2, 2023
390742c
remove unused import
MajedAlaitwniCap Nov 2, 2023
803867d
Merge branch 'THR-18-dev-feature-h5p-editor' of https://github.com/hp…
MajedAlaitwniCap Nov 2, 2023
72f713d
added H5p File Response interface
MajedAlaitwniCap Nov 2, 2023
3fd8d88
create tests h5p-content.entity
casparneumann-cap Nov 2, 2023
da8e01f
Merge branch 'THR-18-dev-feature-h5p-editor' of https://github.com/hp…
casparneumann-cap Nov 2, 2023
276feae
add GetLibraryFile dto
MajedAlaitwniCap Nov 2, 2023
e9f6f4b
Merge branch 'THR-18-dev-feature-h5p-editor' of https://github.com/hp…
MajedAlaitwniCap Nov 2, 2023
2336b3f
rename entity file
MajedAlaitwniCap Nov 2, 2023
d395712
Merge branch 'main' into THR-18-dev-feature-h5p-editor
MajedAlaitwniCap Nov 2, 2023
8ab5968
Create own GetLibraryFile Dto for UC
casparneumann-cap Nov 2, 2023
b03c0ff
Revert post.body.params.transform-pipe & logging
casparneumann-cap Nov 2, 2023
ee74358
Merge branch 'main' into THR-18-dev-feature-h5p-editor
casparneumann-cap Nov 3, 2023
10bcddb
Merge branch 'main' into THR-18-dev-feature-h5p-editor
casparneumann-cap Nov 3, 2023
7918dfc
bugfix post.body.params.transform-pipe
casparneumann-cap Nov 3, 2023
6c6f862
add return undefined
casparneumann-cap Nov 3, 2023
d38d6d9
quick fix: transformed can be undefined
casparneumann-cap Nov 3, 2023
0c992fb
adapt userMcok to updated ICurrentUser Interface
casparneumann-cap Nov 3, 2023
ab7b5d4
Restructure AjaxPostBodyParamsTransformPipe
casparneumann-cap Nov 3, 2023
34f61b4
quick fix nest-cli.json
casparneumann-cap Nov 3, 2023
93ee321
Merge branch 'main' into THR-18-dev-feature-h5p-editor
MajedAlaitwniCap Nov 6, 2023
a8242f6
revert wrong Changes
MajedAlaitwniCap Nov 6, 2023
f1aac17
Merge branch 'THR-18-dev-feature-h5p-editor' of https://github.com/hp…
MajedAlaitwniCap Nov 6, 2023
c8dca2a
Merge branch 'main' into THR-18-dev-feature-h5p-editor
casparneumann-cap Nov 7, 2023
8631541
Comment out end-to-end tests in push
casparneumann-cap Nov 7, 2023
6d0c477
Update push.yml
casparneumann-cap Nov 7, 2023
a83cdb4
Update push.yml: add legacy e2e test again
casparneumann-cap Nov 7, 2023
0c87037
Merge branch 'main' into THR-18-dev-feature-h5p-editor
casparneumann-cap Nov 8, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ansible/roles/schulcloud-server-h5p/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
- name: H5pEditorService
- name: H5PEditorProvider
kubernetes.core.k8s:
kubeconfig: ~/.kube/config
namespace: "{{ NAMESPACE }}"
Expand Down
1 change: 1 addition & 0 deletions apps/server/src/apps/h5p-editor.app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ async function bootstrap() {
const nestExpress = express();

const nestExpressAdapter = new ExpressAdapter(nestExpress);

const nestApp = await NestFactory.create(H5PEditorModule, nestExpressAdapter);
// WinstonLogger
nestApp.useLogger(await nestApp.resolve(LegacyLogger));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
import { DeepMocked, createMock } from '@golevelup/ts-jest';
import { H5PAjaxEndpoint } from '@lumieducation/h5p-server';
import { EntityManager } from '@mikro-orm/core';
import { HttpStatus, INestApplication } from '@nestjs/common';
import { Test } from '@nestjs/testing';
import { S3ClientAdapter } from '@shared/infra/s3-client';
import { TestApiClient, UserAndAccountTestFactory } from '@shared/testing';
import { H5PEditorTestModule } from '../../h5p-editor-test.module';
import { H5P_CONTENT_S3_CONNECTION, H5P_LIBRARIES_S3_CONNECTION } from '../../h5p-editor.config';

describe('H5PEditor Controller (api)', () => {
let app: INestApplication;
let em: EntityManager;
let testApiClient: TestApiClient;

let ajaxEndpoint: DeepMocked<H5PAjaxEndpoint>;

beforeAll(async () => {
const module = await Test.createTestingModule({
imports: [H5PEditorTestModule],
})
.overrideProvider(H5P_CONTENT_S3_CONNECTION)
.useValue(createMock<S3ClientAdapter>())
.overrideProvider(H5P_LIBRARIES_S3_CONNECTION)
.useValue(createMock<S3ClientAdapter>())
.overrideProvider(H5PAjaxEndpoint)
.useValue(createMock<H5PAjaxEndpoint>())
.compile();

app = module.createNestApplication();
await app.init();
em = app.get(EntityManager);
ajaxEndpoint = app.get(H5PAjaxEndpoint);
testApiClient = new TestApiClient(app, 'h5p-editor');
});

afterEach(() => {
jest.resetAllMocks();
});

afterAll(async () => {
await app.close();
});

describe('when calling AJAX GET', () => {
describe('when user not exists', () => {
it('should respond with unauthorized exception', async () => {
const response = await testApiClient.get('ajax');

expect(response.statusCode).toEqual(HttpStatus.UNAUTHORIZED);
expect(response.body).toEqual({
type: 'UNAUTHORIZED',
title: 'Unauthorized',
message: 'Unauthorized',
code: 401,
});
});
});

describe('when user is logged in', () => {
const createStudent = () => UserAndAccountTestFactory.buildStudent();

const setup = async () => {
const { studentAccount, studentUser } = createStudent();

await em.persistAndFlush([studentAccount, studentUser]);
em.clear();

const loggedInClient = await testApiClient.login(studentAccount);

return { loggedInClient, studentUser };
};

it('should call H5PAjaxEndpoint', async () => {
const {
loggedInClient,
studentUser: { id },
} = await setup();

const dummyResponse = {
apiVersion: { major: 1, minor: 1 },
details: [],
libraries: [],
outdated: false,
recentlyUsed: [],
user: 'DummyUser',
};

ajaxEndpoint.getAjax.mockResolvedValueOnce(dummyResponse);

const response = await loggedInClient.get(`ajax?action=content-type-cache`);

expect(response.statusCode).toEqual(HttpStatus.OK);
expect(response.body).toEqual(dummyResponse);
expect(ajaxEndpoint.getAjax).toHaveBeenCalledWith(
'content-type-cache',
undefined, // MachineName
undefined, // MajorVersion
undefined, // MinorVersion
'de', // Language
expect.objectContaining({ id })
);
});
});

describe('when calling AJAX POST', () => {
describe('when user not exists', () => {
it('should respond with unauthorized exception', async () => {
const response = await testApiClient.post('ajax');

expect(response.statusCode).toEqual(HttpStatus.UNAUTHORIZED);
expect(response.body).toEqual({
type: 'UNAUTHORIZED',
title: 'Unauthorized',
message: 'Unauthorized',
code: 401,
});
});
});

describe('when user is logged in', () => {
const createStudent = () => UserAndAccountTestFactory.buildStudent();

const setup = async () => {
const { studentAccount, studentUser } = createStudent();

await em.persistAndFlush([studentAccount, studentUser]);
em.clear();

const loggedInClient = await testApiClient.login(studentAccount);

return { loggedInClient, studentUser };
};

it('should call H5PAjaxEndpoint', async () => {
const {
loggedInClient,
studentUser: { id },
} = await setup();

const dummyResponse = [
{
majorVersion: 1,
minorVersion: 2,
metadataSettings: {},
name: 'Dummy Library',
restricted: false,
runnable: true,
title: 'Dummy Library',
tutorialUrl: '',
uberName: 'dummyLibrary-1.1',
},
];

const dummyBody = { contentId: 'id', field: 'field', libraries: ['dummyLibrary-1.0'], libraryParameters: '' };

ajaxEndpoint.postAjax.mockResolvedValueOnce(dummyResponse);

const response = await loggedInClient.post(`ajax?action=libraries`, dummyBody);

expect(response.statusCode).toEqual(HttpStatus.CREATED);
expect(response.body).toEqual(dummyResponse);
expect(ajaxEndpoint.postAjax).toHaveBeenCalledWith(
'libraries',
dummyBody,
'de',
expect.objectContaining({ id }),
undefined,
undefined,
undefined,
undefined,
undefined
);
});
});
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import { DeepMocked, createMock } from '@golevelup/ts-jest/lib/mocks';
import { EntityManager, ObjectId } from '@mikro-orm/mongodb';
import { ExecutionContext, INestApplication } from '@nestjs/common';
import { Test } from '@nestjs/testing';
import { Permission } from '@shared/domain';
import { S3ClientAdapter } from '@shared/infra/s3-client';
import { cleanupCollections, mapUserToCurrentUser, roleFactory, schoolFactory, userFactory } from '@shared/testing';
import { ICurrentUser } from '@src/modules/authentication';
import { JwtAuthGuard } from '@src/modules/authentication/guard/jwt-auth.guard';
import { Request } from 'express';
import request from 'supertest';
import { H5PEditorTestModule } from '../../h5p-editor-test.module';
import { H5P_CONTENT_S3_CONNECTION, H5P_LIBRARIES_S3_CONNECTION } from '../../h5p-editor.config';
import { H5PEditorUc } from '../../uc/h5p.uc';

class API {
constructor(private app: INestApplication) {
this.app = app;
}

async deleteH5pContent(contentId: string) {
return request(this.app.getHttpServer()).post(`/h5p-editor/delete/${contentId}`);
}
}

const setup = () => {
const contentId = new ObjectId(0).toString();
const notExistingContentId = new ObjectId(1).toString();
const badContentId = '';

return { contentId, notExistingContentId, badContentId };
};

describe('H5PEditor Controller (api)', () => {
let app: INestApplication;
let api: API;
let em: EntityManager;
let currentUser: ICurrentUser;
let h5PEditorUc: DeepMocked<H5PEditorUc>;

beforeAll(async () => {
const module = await Test.createTestingModule({
imports: [H5PEditorTestModule],
})
.overrideGuard(JwtAuthGuard)
.useValue({
canActivate(context: ExecutionContext) {
const req: Request = context.switchToHttp().getRequest();
req.user = currentUser;
return true;
},
})
.overrideProvider(H5P_CONTENT_S3_CONNECTION)
.useValue(createMock<S3ClientAdapter>())
.overrideProvider(H5P_LIBRARIES_S3_CONNECTION)
.useValue(createMock<S3ClientAdapter>())
.overrideProvider(H5PEditorUc)
.useValue(createMock<H5PEditorUc>())
.compile();

app = module.createNestApplication();
await app.init();
h5PEditorUc = module.get(H5PEditorUc);

api = new API(app);
em = module.get(EntityManager);
});

afterAll(async () => {
await app.close();
});

describe('delete h5p content', () => {
beforeEach(async () => {
await cleanupCollections(em);
const school = schoolFactory.build();
const roles = roleFactory.buildList(1, {
permissions: [Permission.FILESTORAGE_CREATE, Permission.FILESTORAGE_VIEW],
});
const user = userFactory.build({ school, roles });

await em.persistAndFlush([user, school]);
em.clear();

currentUser = mapUserToCurrentUser(user);
});
describe('with valid request params', () => {
it('should return 200 status', async () => {
const { contentId } = setup();

h5PEditorUc.deleteH5pContent.mockResolvedValueOnce(true);
const response = await api.deleteH5pContent(contentId);
expect(response.status).toEqual(201);
});
});
describe('with bad request params', () => {
it('should return 500 status', async () => {
const { notExistingContentId } = setup();

h5PEditorUc.deleteH5pContent.mockRejectedValueOnce(new Error('Could not delete H5P content'));
const response = await api.deleteH5pContent(notExistingContentId);
expect(response.status).toEqual(500);
});
});
});
});
Loading
Loading