Skip to content

Instantly share code, notes, and snippets.

@dimensi
Created April 19, 2018 17:34
Show Gist options
  • Save dimensi/cbcc77956ecf8d4ca204c747a9dd8a8a to your computer and use it in GitHub Desktop.
Save dimensi/cbcc77956ecf8d4ca204c747a9dd8a8a to your computer and use it in GitHub Desktop.
Test from microblog on koa.js
import test from 'ava'
import supertest from 'supertest'
process.env.MONGODB_APPLICATION_USER = 'admin'
process.env.MONGODB_APPLICATION_DATABASE = 'admin'
process.env.MONGODB_APPLICATION_PASS = 'qweqwe'
process.env.MONGODB_HOST = 'localhost'
const server = require('../index')
let listen = null
let db = null
test.before(async t => {
const inst = await server
listen = inst.app.listen()
db = inst.db
await inst.db.model('User').remove({})
t.pass()
})
test.after('cleanup', async t => {
await db.model('User').remove({})
})
const createRequest = () => supertest(listen)
test.serial('register', async t => {
const res = await createRequest()
.post('/auth/register')
.send({
email: '[email protected]',
password: 'qweqwe',
})
t.is(res.status, 200)
})
test.serial('login', async t => {
const res = await createRequest()
.post('/auth/login')
.send({
email: '[email protected]',
password: 'qweqwe',
})
t.is(res.status, 200)
})
import test from 'ava'
import supertest from 'supertest'
import slugify from 'slug'
process.env.MONGODB_APPLICATION_USER = 'admin'
process.env.MONGODB_APPLICATION_DATABASE = 'admin'
process.env.MONGODB_APPLICATION_PASS = 'qweqwe'
process.env.MONGODB_HOST = 'localhost'
const server = require('../index')
let listen = null
let db = null
let cookie = null
test.before(async t => {
const inst = await server
listen = inst.app.listen()
db = inst.db
await inst.db.model('Post').remove({})
const userData = {
email: '[email protected]',
password: 'qweqwe',
}
const User = inst.db.model('User')
await User.register(new User(userData), userData.password)
const res = await supertest(listen)
.post('/auth/login')
.send(userData)
cookie = res.header['set-cookie']
t.pass()
})
test.after('cleanup', async t => {
await Promise.all([db.model('Post').remove({}), db.model('User').remove({})])
})
const createRequest = () => supertest(listen)
test.serial('/blog/create', async t => {
const res = await createRequest()
.post('/blog/create')
.set('Cookie', cookie)
.send({
text: 'how are you?',
meta: {
tags: ['hello', 'my new tag'],
title: 'Еще один пост',
},
})
.expect(200)
const slug = slugify('Еще один пост').toLowerCase()
t.is(res.body.slug, slug)
})
test.serial('/blog/:slug', async t => {
const slug = slugify('Еще один пост').toLowerCase()
const res = await createRequest()
.get(`/blog/${slug}`)
.expect(200)
t.regex(res.body.author.email, /test/)
t.is(res.body.slug, slug)
})
test.serial('patch /blog/:slug', async t => {
const slug = slugify('Еще один пост').toLowerCase()
const res = await createRequest()
.patch(`/blog/${slug}`)
.set('Cookie', cookie)
.send({
text: 'good work!',
})
.expect(200)
t.is(res.body.md, 'good work!')
const res2 = await createRequest()
.patch(`/blog/${slug}`)
.set('Cookie', cookie)
.send({
meta: {
title: 'Обновленный пост',
},
})
.expect(200)
const newSlug = slugify('Обновленный пост').toLowerCase()
t.is(res2.body.slug, newSlug)
})
test.serial('delete /blog:slug', async t => {
const title = 'Пост который исчезнет'
const slug = slugify(title).toLowerCase()
await createRequest()
.post('/blog/create')
.set('Cookie', cookie)
.send({
text: 'my super text',
meta: {
tags: ['hello', 'my new tag'],
title,
},
})
.expect(200)
const res = await createRequest()
.del(`/blog/${slug}`)
.set('Cookie', cookie)
.expect(200)
t.is(res.body.slug, slug)
})
test.serial('create many posts', async t => {
const baseTitle = 'Пост #'
t.plan(15)
for (let x = 0; x < 15; x++) {
const title = `${baseTitle} ${x + 1}`
const res = await createRequest()
.post('/blog/create')
.set('Cookie', cookie)
.send({
text: `Text from post ${x + 1}`,
meta: {
tags: ['common tag', title],
title: baseTitle,
},
})
.expect(200)
const extractNumber = res.body.slug.match(/-\d+$/)
if (x === 0 && extractNumber === null) {
t.pass()
} else {
const numberFromSlug = Number(extractNumber[0].replace('-', ''))
t.is(x + 1, numberFromSlug)
}
}
})
test('/blog/all', async t => {
const res = await createRequest()
.get('/blog/all')
.expect(200)
t.is(res.body.total, 16)
})
test('/blog/all with query', async t => {
const res = await createRequest()
.get('/blog/all')
.query({ tags: 'common tag' })
.expect(200)
t.is(res.body.total, 15)
const res2 = await createRequest()
.get('/blog/all')
.query({ tags: 'hello' })
.expect(200)
t.is(res2.body.total, 1)
const res3 = await createRequest()
.get('/blog/all')
.query({ tags: 'hello,common tag' })
.expect(200)
t.is(res3.body.total, 16)
})
test('/blog/tags', async t => {
const res = await createRequest()
.get('/blog/tags')
.expect(200)
const expectArr = ['hello', 'my new tag', 'common tag']
const baseTitle = 'Пост #'
for (let x = 0; x < 15; x++) {
expectArr.push(`${baseTitle} ${x + 1}`)
}
t.deepEqual(res.body, expectArr)
})
import path from 'path'
import test from 'ava'
import supertest from 'supertest'
process.env.MONGODB_APPLICATION_USER = 'admin'
process.env.MONGODB_APPLICATION_DATABASE = 'admin'
process.env.MONGODB_APPLICATION_PASS = 'qweqwe'
process.env.MONGODB_HOST = 'localhost'
const server = require('../index')
let listen = null
test.before(async t => {
const inst = await server
listen = inst.app.listen()
t.pass()
})
const createRequest = () => supertest(listen)
test('Upload file', async t => {
const res = await createRequest()
.post('/file/create')
.attach('file', path.join(__dirname, 'files/test-image.png'))
.expect(200)
t.regex(res.body.filename, /test-image/)
})
test('Get file', async t => {
const res = await createRequest()
.get('/static/test-image.png')
console.log(res.body)
t.is(res.status, 200)
t.is(res.header['content-type'], 'image/png')
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment