Browse Source

feat: Tests for basic activity

tags/v0.1.0
Dale 2 months ago
parent
commit
ec36ef4f4f
5 changed files with 322 additions and 0 deletions
  1. +20
    -0
      test/const.ts
  2. +89
    -0
      test/server/api/v1/v1.01.users.spec.ts
  3. +41
    -0
      test/server/api/v1/v1.02.auth.spec.ts
  4. +84
    -0
      test/server/api/v1/v1.03.activities.spec.ts
  5. +88
    -0
      test/server/api/v1/v1.04.channels.spec.ts

+ 20
- 0
test/const.ts View File

@@ -0,0 +1,20 @@
import md5 from 'js-md5';

export const testUsername = 'test_user';
export const testPassword = 'test_password';
export const testEmail = 'test@test.test';
export const testDisplayName = 'Test User';
export const testBio = 'This is a test user';
export const testEmailHash = md5(testEmail);
export const userLoginObject = {
username: testUsername,
password: testPassword,
};

export const testActivityName = 'Quake';
export const testActivityVerb = 'playing';
export const testActivityNewVerb = 'fragging in';

export const testChannelTitle = 'Test Channel';
export const testChannelSlug = '/test-channel';
export const testChannelDescription = 'This is a test channel';

+ 89
- 0
test/server/api/v1/v1.01.users.spec.ts View File

@@ -0,0 +1,89 @@
import { expect } from 'chai';
import { IUserCreation, IUserSafe } from 'miracle-tv-shared/src/types/api/v1/interfaces/IUser';
import 'mocha';
import app from 'server/server';
import request from 'supertest';
import { testUsername, testPassword, testEmail, testEmailHash, testBio, testDisplayName } from 'test/const';

describe('User module: Create and modify user', () => {
let userToken: string;
let userId: string;

const userLoginObject = {
username: testUsername,
password: testPassword,
};

const userCreateObject: IUserCreation = {
...userLoginObject,
email: testEmail,
};

it('Should create new user and return token', (done) => {
request(app)
.post('/api/v1/auth/sign-up')
.send(userCreateObject)
.expect(200)
.end((err, res) => {
expect(err).to.be.null;
expect(res).to.have.nested.property('body.data.token');
userToken = res.body.data.token;
done();
});
});

it('Should return own user', (done) => {
request(app)
.get('/api/v1/auth/me')
.set('Authorization', `Token ${userToken}`)
.expect(200)
.end((err, res) => {
expect(err).to.be.null;
expect(res).to.have.nested.property('body.data');
const data = res.body.data as IUserSafe;
expect(data.username).to.equal(testUsername);
expect(data.emailHash).to.equal(testEmailHash);
userId = data.id;
done();
});
});

it('Should list user in api route', (done) => {
request(app)
.get('/api/v1/users')
.expect(200)
.end((err, res) => {
expect(err).to.be.null;
expect(res).to.have.nested.property('body.data');
const data = res.body.data as IUserSafe[];
const ourUser = data.find((user: IUserSafe) => user.username === testUsername);
expect(ourUser).to.not.be.null;
expect(ourUser.username).to.equal(testUsername);
expect(ourUser.emailHash).to.equal(testEmailHash);
done();
});
});

it('Should modify user', (done) => {
const userModObject = {
bio: testBio,
displayName: testDisplayName,
};

expect(userId).to.not.be.null;

request(app)
.put(`/api/v1/users/${userId}`)
.set('Authorization', `Token ${userToken}`)
.send(userModObject)
.expect(200)
.end((err, res) => {
expect(err).to.be.null;
expect(res).to.have.nested.property('body.data');
const data = res.body.data as IUserSafe;
expect(data.bio).to.equal(testBio);
expect(data.displayName).to.equal(testDisplayName);
done();
});
});
});

+ 41
- 0
test/server/api/v1/v1.02.auth.spec.ts View File

@@ -0,0 +1,41 @@
import 'mocha';
import { expect } from 'chai';
import request from 'supertest';

import app, { appDB } from 'server/server';
import { testUsername, testPassword } from 'test/const';

describe('Auth module: Login and Auth', () => {
let userToken: string;

const userLoginObject = {
username: testUsername,
password: testPassword,
};

it('Should login with password', (done) => {
request(app)
.post('/api/v1/auth/login')
.send(userLoginObject)
.expect(200)
.end((err, res) => {
expect(err).to.be.null;
expect(res).to.have.nested.property('body.data');
const data = res.body.data as { token?: string };
expect(data).to.have.haveOwnProperty('token');
userToken = data.token;
done();
});
});

it('Should auth with new token', (done) => {
request(app)
.get('/api/v1/auth/me')
.set('Authorization', `Token ${userToken}`)
.expect(200)
.end((err) => {
expect(err).to.be.null;
done();
});
});
});

+ 84
- 0
test/server/api/v1/v1.03.activities.spec.ts View File

@@ -0,0 +1,84 @@
import 'mocha';
import { expect } from 'chai';
import request from 'supertest';

import IActivity from 'miracle-tv-shared/src/types/api/v1/interfaces/IActivity';

import app, { appDB } from 'server/server';
import { testActivityName, userLoginObject, testActivityVerb, testActivityNewVerb } from 'test/const';

describe('Activities: Create and modify activities', () => {
let userToken: string;
let activity: IActivity;

const newActivityObject = {
name: testActivityName,
verb: testActivityVerb,
};

it('Should login before accessing channels', (done) => {
request(app)
.post('/api/v1/auth/login')
.send(userLoginObject)
.expect(200)
.end((err, res) => {
expect(err).to.be.null;
expect(res).to.have.nested.property('body.data.token');
const data = res.body.data as { token: string };
expect(data.token).to.exist;
userToken = data.token;
expect(userToken).to.exist;
done();
});
});

it('Should create new activity', (done) => {
request(app)
.post('/api/v1/activities')
.set('Authorization', `Token ${userToken}`)
.send(newActivityObject)
.expect(201)
.end((err, res) => {
expect(err).to.be.null;
expect(res).to.have.nested.property('body.data');
const data = res.body.data as IActivity;
expect(data.name).to.be.equal(testActivityName);
expect(data.verb).to.be.equal(testActivityVerb);
activity = data;
done();
});
});

it('Should list all activities', (done) => {
request(app)
.get('/api/v1/activities')
.expect(200)
.end((err, res) => {
expect(err).to.be.null;
expect(res).to.have.nested.property('body.data');
const data = res.body.data as IActivity[];
expect(data).to.be.an('array');
expect(data).to.have.lengthOf(1);
expect(data[0].id).to.be.equal(activity.id);
done();
});
});

it('Should modify own activity', (done) => {
request(app)
.put(`/api/v1/activities/${activity.id}`)
.set('Authorization', `Token ${userToken}`)
.send({ name: activity.name, verb: testActivityNewVerb })
.end((err, res) => {
expect(err).to.be.null;
expect(res).to.have.nested.property('body.data');
const data = res.body.data as IActivity;
expect(data.verb).to.be.equal(testActivityNewVerb);
done();
});
});

it('Should delete activity', (done) => {
done();
});
});

+ 88
- 0
test/server/api/v1/v1.04.channels.spec.ts View File

@@ -0,0 +1,88 @@
import 'mocha';
import { expect } from 'chai';
import IChannel from 'miracle-tv-shared/src/types/api/v1/interfaces/IChannel';
import request from 'supertest';
import { testUsername, testPassword, testChannelTitle, testChannelSlug } from 'test/const';
import app, { appDB } from 'server/server';
import IActivity from 'miracle-tv-shared/src/types/api/v1/interfaces/IActivity';

const userLoginObject = {
username: testUsername,
password: testPassword,
};

describe('Channels module: Create and Modify Channel', () => {
after(() => appDB.destroy());
let userToken: string;
let channelId: string;

const newChannelObject = {
title: testChannelTitle,
slug: testChannelSlug,
};

it('Should login before accessing channels', (done) => {
request(app)
.post('/api/v1/auth/login')
.send(userLoginObject)
.expect(200)
.end((err, res) => {
expect(err).to.be.null;
expect(res).to.have.nested.property('body.data.token');
const data = res.body.data as { token: string };
expect(data.token).to.exist;
userToken = data.token;
expect(userToken).to.exist;
done();
});
});

it('Should create new channel', (done) => {
request(app)
.get('/api/v1/activities')
.expect(200)
.end((err, res) => {
expect(err).to.be.null;
expect(res).to.have.nested.property('body.data');
const data = res.body.data as IActivity[];
expect(data).to.be.an('array');
expect(data).to.have.lengthOf(1);
const activityId = data[0].id;
expect(activityId).to.be.a('string');

request(app)
.post('/api/v1/channels')
.set('Authorization', `Token ${userToken}`)
.send({
...newChannelObject,
activity_id: activityId,
})
.end((err, res) => {
expect(err).to.be.null;
expect(res).to.have.nested.property('body.data');
const data = res.body.data as IChannel;
expect(data.title).to.be.equal(testChannelTitle);
expect(data.slug).to.be.equal(testChannelSlug);
expect(data.activity_id).to.be.equal(activityId);
expect(data.created_by.username).to.be.equal(testUsername);
channelId = data.id;
done();
});
});
});

it('Should list all channels', (done) => {
request(app)
.get('/api/v1/channels')
.expect(200)
.end((err, res) => {
expect(err).to.be.null;
expect(res).to.have.nested.property('body.data');
const data = res.body.data as IChannel[];
expect(data).to.be.an('array');
expect(data).to.have.lengthOf(1);
expect(data[0].id).to.be.equal(channelId);
done();
});
});
});

Loading…
Cancel
Save