<commit_before>/* eslint-disable no-unused-vars,no-undef */
const nock = require('nock');
const chai = require('chai');
const Subscription = require('models/subscription');
const config = require('config');
const redis = require('redis');
const { sleep } = require('sleep');
const { ROLES } = require('./utils/test.constants');
const { getTestServer } = require('./utils/test-server');
const { validRedisMessage } = require('./utils/helpers');

nock.disableNetConnect();
nock.enableNetConnect(process.env.HOST_IP);

const should = chai.should();

const CHANNEL = config.get('apiGateway.queueName');

let requester;

const assertSubscriptionResponse = async (response) => {
    response.status.should.equal(200);
    response.body.should.have.property('data').and.be.an('object');

    const databaseSubscriptions = await Subscription.find({}).exec();
    databaseSubscriptions.should.be.an('array').and.have.length(1);

    const subscriptionOne = databaseSubscriptions[0];
    const responseSubscription = response.body.data;

    responseSubscription.id.should.equal(subscriptionOne.id);
    responseSubscription.attributes.should.have.property('confirmed').and.equal(subscriptionOne.confirmed);
    responseSubscription.attributes.should.have.property('datasets').and.be.an('array').and.length(1).and.contains(subscriptionOne.datasets[0]);
    responseSubscription.attributes.should.have.property('createdAt').and.be.a('string');
    responseSubscription.attributes.should.have.property('datasetsQuery').and.be.an('array').and.length(0);
    responseSubscription.attributes.should.have.property('env').and.equal(subscriptionOne.env);
    responseSubscription.attributes.should.have.property('language').and.equal(subscriptionOne.language);
    responseSubscription.attributes.should.have.property('params').and.deep.equal(subscriptionOne.params);
    responseSubscription.attributes.resource.should.have.property('content').and.equal(subscriptionOne.resource.content);
    responseSubscription.attributes.resource.should.have.property('type').and.equal(subscriptionOne.resource.type);

    process.on('unhandledRejection', (err) => should.fail(err));
};

describe('Create subscriptions tests', () => {
    before(async () => {
        if (process.env.NODE_ENV !== 'test') {
            throw Error(`Running the test suite with NODE_ENV ${process.env.NODE_ENV} may result in permanent data loss. Please use NODE_ENV=test.`);
        }

        this.channel = redis.createClient({ url: config.get('redis.url') });
        this.channel.subscribe(CHANNEL);

        requester = await getTestServer();

        await Subscription.deleteMany({}).exec();
    });

    it('Create a subscription with no dataset or datasetsQuery should return a 400 error', async () => {
        this.channel.on('message', () => should.fail('should not be called'));

        const response = await requester
            .post(`/api/v1/subscriptions`)
            .send({
                loggedUser: ROLES.ADMIN
            });

        response.status.should.equal(400);
        response.body.should.have.property('errors').and.be.an('array').and.length(1);
        response.body.errors[0].should.have.property('status').and.equal(400);
        response.body.errors[0].should.have.property('detail').and.equal('Datasets or datasetsQuery required');

        sleep(1);
    });

    it('Create a subscription with no language should return a 400 error', async () => {
        this.channel.on('message', () => should.fail('should not be called'));

        const response = await requester
            .post(`/api/v1/subscriptions`)
            .send({
                datasets: ['123456789'],
                loggedUser: ROLES.ADMIN
            });

        response.status.should.equal(400);
        response.body.should.have.property('errors').and.be.an('array').and.length(1);
        response.body.errors[0].should.have.property('status').and.equal(400);
        response.body.errors[0].should.have.property('detail').and.equal('Language required');

        sleep(1);
    });

    it('Create a subscription with no resource should return a 400 error', async () => {
        this.channel.on('message', () => should.fail('should not be called'));

        const response = await requester
            .post(`/api/v1/subscriptions`)
            .send({
                datasets: ['123456789'],
                language: 'en',
                loggedUser: ROLES.ADMIN
            });

        response.status.should.equal(400);
        response.body.should.have.property('errors').and.be.an('array').and.length(1);
        response.body.errors[0].should.have.property('status').and.equal(400);
        response.body.errors[0].should.have.property('detail').and.equal('Resource required');

        sleep(1);
    });

    it('Create a subscription with no params should return a 400 error', async () => {
        this.channel.on('message', () => should.fail('should not be called'));

        const response = await requester
            .post(`/api/v1/subscriptions`)
            .send({
                datasets: ['123456789'],
                language: 'en',
                resource: {
                    type: 'EMAIL',
                    content: '<EMAIL>'
                },
                loggedUser: ROLES.ADMIN
            });

        response.status.should.equal(400);
        response.body.should.have.property('errors').and.be.an('array').and.length(1);
        response.body.errors[0].should.have.property('status').and.equal(400);
        response.body.errors[0].should.have.property('detail').and.equal('Params required');

        sleep(1);
    });

    it('Create a subscription with the basic required fields should return a 200, create a subscription and emit a redis message (happy Case)', async () => {
        this.channel.on('message', validRedisMessage({
            template: 'subscription-confirmation-en',
            application: 'gfw',
            language: 'en',
        }));

        const response = await requester
            .post(`/api/v1/subscriptions`)
            .send({
                datasets: ['123456789'],
                language: 'en',
                resource: {
                    type: 'EMAIL',
                    content: '<EMAIL>'
                },
                params: {
                    geostore: '35a6d982388ee5c4e141c2bceac3fb72'
                },
                loggedUser: ROLES.ADMIN
            });

        await assertSubscriptionResponse(response);
    });

    it('Create a subscription with the basic required fields with application = "test" should return a 200, create a subscription and emit a redis message(happy case)', async () => {
        this.channel.on('message', validRedisMessage({
            template: 'subscription-confirmation-test-ru',
            application: 'gfw',
            language: 'ru',
        }));

        const response = await requester
            .post(`/api/v1/subscriptions`)
            .send({
                datasets: ['123456789'],
                language: 'ru',
                resource: {
                    type: 'EMAIL',
                    content: '<EMAIL>'
                },
                application: 'test',
                params: {
                    geostore: '35a6d982388ee5c4e141c2bceac3fb72'
                },
                loggedUser: ROLES.ADMIN
            });

        await assertSubscriptionResponse(response);<commit_msg>Fix creat sub test<commit_after>/* eslint-disable no-unused-vars,no-undef */
const nock = require('nock');
const chai = require('chai');
const Subscription = require('models/subscription');
const config = require('config');
const redis = require('redis');
const { sleep } = require('sleep');
const { ROLES } = require('./utils/test.constants');
const { getTestServer } = require('./utils/test-server');
const { validRedisMessage } = require('./utils/helpers');

nock.disableNetConnect();
nock.enableNetConnect(process.env.HOST_IP);

const should = chai.should();

const CHANNEL = config.get('apiGateway.queueName');

let requester;

const assertSubscriptionResponse = async (response) => {
    response.status.should.equal(200);
    response.body.should.have.property('data').and.be.an('object');

    const databaseSubscriptions = await Subscription.find({}).exec();
    databaseSubscriptions.should.be.an('array').and.have.length(1);

    const subscriptionOne = databaseSubscriptions[0];
    const responseSubscription = response.body.data;

    responseSubscription.id.should.equal(subscriptionOne.id);
    responseSubscription.attributes.should.have.property('confirmed').and.equal(subscriptionOne.confirmed);
    responseSubscription.attributes.should.have.property('datasets').and.be.an('array').and.length(1).and.contains(subscriptionOne.datasets[0]);
    responseSubscription.attributes.should.have.property('createdAt').and.be.a('string');
    responseSubscription.attributes.should.have.property('datasetsQuery').and.be.an('array').and.length(0);
    responseSubscription.attributes.should.have.property('env').and.equal(subscriptionOne.env);
    responseSubscription.attributes.should.have.property('language').and.equal(subscriptionOne.language);
    responseSubscription.attributes.should.have.property('params').and.deep.equal(subscriptionOne.params);
    responseSubscription.attributes.resource.should.have.property('content').and.equal(subscriptionOne.resource.content);
    responseSubscription.attributes.resource.should.have.property('type').and.equal(subscriptionOne.resource.type);

    process.on('unhandledRejection', (err) => should.fail(err));
};

describe('Create subscriptions tests', () => {
    before(async () => {
        if (process.env.NODE_ENV !== 'test') {
            throw Error(`Running the test suite with NODE_ENV ${process.env.NODE_ENV} may result in permanent data loss. Please use NODE_ENV=test.`);
        }

        this.channel = redis.createClient({ url: config.get('redis.url') });
        this.channel.subscribe(CHANNEL);

        requester = await getTestServer();

        await Subscription.deleteMany({}).exec();
    });

    it('Create a subscription with no dataset or datasetsQuery should return a 400 error', async () => {
        this.channel.on('message', () => should.fail('should not be called'));

        const response = await requester
            .post(`/api/v1/subscriptions`)
            .send({
                loggedUser: ROLES.ADMIN
            });

        response.status.should.equal(400);
        response.body.should.have.property('errors').and.be.an('array').and.length(1);
        response.body.errors[0].should.have.property('status').and.equal(400);
        response.body.errors[0].should.have.property('detail').and.equal('Datasets or datasetsQuery required');

        sleep(1);
    });

    it('Create a subscription with no language should return a 400 error', async () => {
        this.channel.on('message', () => should.fail('should not be called'));

        const response = await requester
            .post(`/api/v1/subscriptions`)
            .send({
                datasets: ['123456789'],
                loggedUser: ROLES.ADMIN
            });

        response.status.should.equal(400);
        response.body.should.have.property('errors').and.be.an('array').and.length(1);
        response.body.errors[0].should.have.property('status').and.equal(400);
        response.body.errors[0].should.have.property('detail').and.equal('Language required');

        sleep(1);
    });

    it('Create a subscription with no resource should return a 400 error', async () => {
        this.channel.on('message', () => should.fail('should not be called'));

        const response = await requester
            .post(`/api/v1/subscriptions`)
            .send({
                datasets: ['123456789'],
                language: 'en',
                loggedUser: ROLES.ADMIN
            });

        response.status.should.equal(400);
        response.body.should.have.property('errors').and.be.an('array').and.length(1);
        response.body.errors[0].should.have.property('status').and.equal(400);
        response.body.errors[0].should.have.property('detail').and.equal('Resource required');

        sleep(1);
    });

    it('Create a subscription with no params should return a 400 error', async () => {
        this.channel.on('message', () => should.fail('should not be called'));

        const response = await requester
            .post(`/api/v1/subscriptions`)
            .send({
                datasets: ['123456789'],
                language: 'en',
                resource: {
                    type: 'EMAIL',
                    content: '<EMAIL>'
                },
                loggedUser: ROLES.ADMIN
            });

        response.status.should.equal(400);
        response.body.should.have.property('errors').and.be.an('array').and.length(1);
        response.body.errors[0].should.have.property('status').and.equal(400);
        response.body.errors[0].should.have.property('detail').and.equal('Params required');

        sleep(1);
    });

    it('Create a subscription with the basic required fields should return a 200, create a subscription and emit a redis message (happy Case)', async () => {
        this.channel.on('message', validRedisMessage({
            template: 'subscription-confirmation-en',
            application: 'gfw',
            language: 'en',
        }));

        const response = await requester
            .post(`/api/v1/subscriptions`)
            .send({
                datasets: ['123456789'],
                language: 'en',
                resource: {
                    type: 'EMAIL',
                    content: '<EMAIL>'
                },
                params: {
                    geostore: '35a6d982388ee5c4e141c2bceac3fb72'
                },
                loggedUser: ROLES.ADMIN
            });

        await assertSubscriptionResponse(response);
    });

    it('Create a subscription with the basic required fields with application = "test" should return a 200, create a subscription and emit a redis message(happy case)', async () => {
        this.channel.on('message', validRedisMessage({
            template: 'subscription-confirmation-test-en',
            application: 'gfw',
            language: 'en',
        }));

        const response = await requester
            .post(`/api/v1/subscriptions`)
            .send({
                datasets: ['123456789'],
                language: 'en',
                resource: {
                    type: 'EMAIL',
                    content: '<EMAIL>'
                },
                application: 'test',
                params: {
                    geostore: '35a6d982388ee5c4e141c2bceac3fb72'
                },
                loggedUser: ROLES.ADMIN
            });

        await assertSubscriptionResponse(response);
{'fraction_non_alphanumeric': 0.14509093349439636, 'fraction_numerical': 0.020535534527884036, 'mean_word_length': 2.5096561469618464, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '660814', 'n_tokens_mistral': 4180, 'n_tokens_neox': 3979, 'n_words': 716}