<commit_before>'use strict';

let users = require('elyoos-server-test-util').user;
let db = require('elyoos-server-test-util').db;
let dbDsl = require('elyoos-server-test-util').dbDSL;
let requestHandler = require('elyoos-server-test-util').requestHandler;
let should = require('chai').should();
let moment = require('moment');

describe('Creating new questions', function () {

    let startTime;

    beforeEach(async function () {
        await dbDsl.init(3);
        startTime = Math.floor(moment.utc().valueOf() / 1000);
    });

    afterEach(function () {
        return requestHandler.logout();
    });

    it('Adding a new question with description', async function () {
        await dbDsl.sendToDb();
        await requestHandler.login(users.validUser);
        let res = await requestHandler.post('/api/user/question', {
            question: 'Das ist eine FragöÖÄäÜü',
            description: 'description',
            topic: ['spiritual', 'education'],
            lang: 'de'
        });
        res.status.should.equal(200);
        res.body.slug.should.equals('das-ist-eine-fragööääüü');

        let resp = await db.cypher().match("(question:Question)<-[:IS_ADMIN]-(user:User)")
            .return(`question, user`).end().send();
        resp.length.should.equals(1);
        resp[0].question.questionId.should.equals(res.body.questionId);
        resp[0].question.question.should.equals('Das ist eine FragöÖÄäÜü');
        resp[0].question.description.should.equals('description');
        resp[0].question.created.should.least(startTime);
        resp[0].question.topic.length.should.equals(2);
        resp[0].question.topic[0].should.equals('spiritual');
        resp[0].question.topic[1].should.equals('education');
        resp[0].question.language.should.equals('de');
        resp[0].user.userId.should.equals('1');
    });

    it('Adding a new question without a description', async function () {
        await dbDsl.sendToDb();
        await requestHandler.login(users.validUser);
        let res = await requestHandler.post('/api/user/question', {
            question: 'Das ist eine FragöÖÄäÜü', topic: ['spiritual', 'education'], lang: 'de'
        });
        res.status.should.equal(200);
        res.body.slug.should.equals('das-ist-eine-fragööääüü');

        let resp = await db.cypher().match("(question:Question)<-[:IS_ADMIN]-(user:User)")
            .return(`question, user`).end().send();
        resp.length.should.equals(1);
        resp[0].question.questionId.should.equals(res.body.questionId);
        resp[0].question.question.should.equals('Das ist eine FragöÖÄäÜü');
        should.not.exist(resp[0].question.description);
        resp[0].question.created.should.least(startTime);
        resp[0].question.topic.length.should.equals(2);
        resp[0].question.topic[0].should.equals('spiritual');
        resp[0].question.topic[1].should.equals('education');
        resp[0].question.language.should.equals('de');
        resp[0].user.userId.should.equals('1');
    });
});<commit_msg>Test if only a logged in user can create a question.<commit_after>'use strict';

let users = require('elyoos-server-test-util').user;
let db = require('elyoos-server-test-util').db;
let dbDsl = require('elyoos-server-test-util').dbDSL;
let requestHandler = require('elyoos-server-test-util').requestHandler;
let should = require('chai').should();
let moment = require('moment');

describe('Creating new questions', function () {

    let startTime;

    beforeEach(async function () {
        await dbDsl.init(3);
        startTime = Math.floor(moment.utc().valueOf() / 1000);
    });

    afterEach(function () {
        return requestHandler.logout();
    });

    it('Adding a new question with description', async function () {
        await dbDsl.sendToDb();
        await requestHandler.login(users.validUser);
        let res = await requestHandler.post('/api/user/question', {
            question: 'Das ist eine FragöÖÄäÜü',
            description: 'description',
            topic: ['spiritual', 'education'],
            lang: 'de'
        });
        res.status.should.equal(200);
        res.body.slug.should.equals('das-ist-eine-fragööääüü');

        let resp = await db.cypher().match("(question:Question)<-[:IS_ADMIN]-(user:User)")
            .return(`question, user`).end().send();
        resp.length.should.equals(1);
        resp[0].question.questionId.should.equals(res.body.questionId);
        resp[0].question.question.should.equals('Das ist eine FragöÖÄäÜü');
        resp[0].question.description.should.equals('description');
        resp[0].question.created.should.least(startTime);
        resp[0].question.topic.length.should.equals(2);
        resp[0].question.topic[0].should.equals('spiritual');
        resp[0].question.topic[1].should.equals('education');
        resp[0].question.language.should.equals('de');
        resp[0].user.userId.should.equals('1');
    });

    it('Adding a new question without a description', async function () {
        await dbDsl.sendToDb();
        await requestHandler.login(users.validUser);
        let res = await requestHandler.post('/api/user/question', {
            question: 'Das ist eine FragöÖÄäÜü', topic: ['spiritual', 'education'], lang: 'de'
        });
        res.status.should.equal(200);
        res.body.slug.should.equals('das-ist-eine-fragööääüü');

        let resp = await db.cypher().match("(question:Question)<-[:IS_ADMIN]-(user:User)")
            .return(`question, user`).end().send();
        resp.length.should.equals(1);
        resp[0].question.questionId.should.equals(res.body.questionId);
        resp[0].question.question.should.equals('Das ist eine FragöÖÄäÜü');
        should.not.exist(resp[0].question.description);
        resp[0].question.created.should.least(startTime);
        resp[0].question.topic.length.should.equals(2);
        resp[0].question.topic[0].should.equals('spiritual');
        resp[0].question.topic[1].should.equals('education');
        resp[0].question.language.should.equals('de');
        resp[0].user.userId.should.equals('1');
    });

    it('Only allowed to add a question as logged in user', async function () {
        await dbDsl.sendToDb();
        let res = await requestHandler.post('/api/user/question', {
            question: 'Das ist eine FragöÖÄäÜü', topic: ['spiritual', 'education'], lang: 'de'
        });
        res.status.should.equal(401);
    });
});
{'fraction_non_alphanumeric': 0.1689398636081835, 'fraction_numerical': 0.012554246745195289, 'mean_word_length': 3.048306148055207, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, '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': '2143257', 'n_tokens_mistral': 2024, 'n_tokens_neox': 1967, 'n_words': 305}