import { afterEach, beforeEach, describe, expect, it } from '@jest/globals';
import { Test, TestingModule } from '@nestjs/testing';
import request = require('supertest');
import { AppModule } from '../src/app.module';
import { INestApplication } from '@nestjs/common';
<%_ if (authenticationType === 'jwt' && !skipUserManagement) { _%>
import { UserLoginDTO } from '../src/service/dto/user-login.dto';
<%_ } _%>

describe('App', () => {
    let app: INestApplication;

    const infoService = {
         activeProfiles : 'dev',
        'display-ribbon-on-profiles': 'dev',
    };
<%_  if (!skipUserManagement && authenticationType !=='oauth2') { _%>
    const testUserLogin: UserLoginDTO = {
        username: 'system',
        password: 'system',
        rememberMe: true
    };
<%_ } else if (authenticationType === 'oauth2') { _%>

    const testRequest: any = {};
<%_ }_%>

    beforeEach(async () => {
        const moduleFixture: TestingModule = await Test.createTestingModule({
            imports: [AppModule],
        }).compile();

        app = moduleFixture.createNestApplication();
        await app.init();
    });

    it('/GET up running info OK', () => request(app.getHttpServer())
        .get('/management/info')
        .expect(200)
        .expect(infoService));

    it('/GET public roles OK', () => request(app.getHttpServer())
        .get('/api/authorities')
        .expect(200));

    it('/GET public users OK', () => request(app.getHttpServer())
        .get('/api/users')
        .expect(200));

    <%_ if (!skipUserManagement && authenticationType !=='oauth2') { _%>
    it<%- databaseTypeMongodb ? '.skip' : ''%>('/POST authenticate get jwt authenticate OK', () => request(app.getHttpServer())
        .post('/api/authenticate')
        .send(testUserLogin)
        .expect(200));
    <%_ } else if (authenticationType === 'oauth2') { _%>
    it('/POST security oauth2 does not perform logout', () => request(app.getHttpServer())
        .post('/api/logout')
        .send(testRequest)
        .expect({})
        .expect(201));

    it('/GET account not logged', () => request(app.getHttpServer())
        .get('/api/account')
        .expect(200)
        .expect({}));
    <%_ } _%>

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