<commit_before>const request = require('supertest');
const jwt = require('jsonwebtoken');

const knex = require('../dbConnect');
const app = require('../server');
const { encodeToken } = require('../auth/utils');

describe('# Authentication', () => {
    beforeEach(async (done) => {
        await knex.migrate.latest();
        await knex.seed.run();
        done();
    });

    it('GET /api/posts without authentication', (done) => {
        request(app)
            .get('/api/posts')
            .expect('Content-type', /json/)
            .expect(403)
            .end((err, res) => {
                if (err) throw err;

                expect(res.body).toEqual({ status: "User is not logged in." });
                done();
            });
    });

    it('GET /api/posts with a valid JWT and return all posts', async (done) => {
        const user = await knex('users').first();
        const token = encodeToken(user);
        const posts =
            await knex('posts')
                .join('users', 'posts.user_id', 'users.id')
                .select('posts.*', 'users.username', 'users.avatar')
                .orderBy('posts.posted_at', 'desc');

        request(app)
            .get('/api/posts')
            .set('Authorization', `Bearer ${token}`)
            .expect('Content-type', /json/)
            .expect(200)
            .end((err, res) => {
                if (err) throw err;

                expect(res.body.posts).toHaveLength(posts.length);
                res.body.posts.forEach((post, idx) => {
                    expect(post).toEqual(expect.objectContaining({
                        id: posts[idx].id,
                        user_id: posts[idx].user_id,
                        username: posts[idx].username,
                        avatar: posts[idx].avatar,
                        image: posts[idx].image,
                    }));
                });
                done();
            });
    });

    it('GET /api/posts with a invalid JWT', async (done) => {
        const user = await knex('users').first();
        const encodeFaultyJwt = ({ id, username, avatar }) => {
            const payload = {
                id,
                username,
                avatar,
            };

            return jwt.sign(payload, 'wrongSecretToken', {
                expiresIn: '14 days',
            });
        };

        const token = encodeFaultyJwt(user);

        request(app)
            .get('/api/posts')
            .set('Authorization', `Bearer ${token}`)
            .expect('Content-type', /json/)
            .end((err, res) => {
                if (err) throw err;

                expect(res.body).toEqual({ status: "Token is invalid or has expired." });
                done();<commit_msg>More tests<commit_after>const request = require('supertest');
const jwt = require('jsonwebtoken');

const knex = require('../dbConnect');
const app = require('../server');
const { encodeToken } = require('../auth/utils');

describe('# Authentication', () => {
    beforeEach(async (done) => {
        await knex.migrate.latest();
        await knex.seed.run();
        done();
    });

    it('GET /api/posts without authentication', (done) => {
        request(app)
            .get('/api/posts')
            .expect('Content-type', /json/)
            .expect(403)
            .end((err, res) => {
                if (err) throw err;

                expect(res.body).toEqual({ status: "User is not logged in." });
                done();
            });
    });

    it('GET /api/posts with a valid JWT and return all posts', async (done) => {
        const user = await knex('users').first();
        const token = encodeToken(user);
        const posts =
            await knex('posts')
                .join('users', 'posts.user_id', 'users.id')
                .select('posts.*', 'users.username', 'users.avatar')
                .orderBy('posts.posted_at', 'desc');

        request(app)
            .get('/api/posts')
            .set('Authorization', `Bearer ${token}`)
            .expect('Content-type', /json/)
            .expect(200)
            .end((err, res) => {
                if (err) throw err;

                expect(res.body.posts).toHaveLength(posts.length);
                res.body.posts.forEach((post, idx) => {
                    expect(post).toEqual(expect.objectContaining({
                        id: posts[idx].id,
                        user_id: posts[idx].user_id,
                        username: posts[idx].username,
                        avatar: posts[idx].avatar,
                        image: posts[idx].image,
                    }));
                });
                done();
            });
    });

    it('GET /api/posts with a invalid JWT', async (done) => {
        const user = await knex('users').first();
        console.log(user)
        const encodeFaultyJwt = ({ id, username, avatar }) => {
            const payload = {
                id,
                username,
                avatar,
            };

            return jwt.sign(payload, 'wrongSecretToken', {
                expiresIn: '14 days',
            });
        };

        const token = encodeFaultyJwt(user);

        request(app)
            .get('/api/posts')
            .set('Authorization', `Bearer ${token}`)
            .expect('Content-type', /json/)
            .end((err, res) => {
                if (err) throw err;

                expect(res.body).toEqual({ status: "Token is invalid or has expired." });
                done();
{'fraction_non_alphanumeric': 0.15686629274470779, 'fraction_numerical': 0.002894879681563235, 'mean_word_length': 1.4481842338352524, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, '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': '1844999', 'n_tokens_mistral': 1548, 'n_tokens_neox': 1473, 'n_words': 323}