Skip to content

Commit d443936

Browse files
authored
v0.2.6 (#111)
* fix: search by word (#108) * fix: search by word * fix: documentation * fix: tests and randomVerse (#110)
1 parent dff2699 commit d443936

24 files changed

Lines changed: 229 additions & 919 deletions

__test__/app.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import express from 'express'
22

3-
import appRoutes from '../routes/index'
4-
53
import '../models/book'
64
import '../models/verse'
75
import '../models/request'
86
import '../models/user'
7+
8+
import { startRedis } from '../controllers/rateLimit'
9+
import appRoutes from '../routes/index'
10+
11+
startRedis()
12+
913
const app = express()
1014

1115
app.use(express.json())

__test__/controllers/book.test.js

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,56 @@ import supertest from 'supertest'
22

33
import Book from '../../models/book'
44
import app from '../app'
5-
import { connect } from '../utils'
5+
import { connect, getUser } from '../utils'
66

77
jest.mock('axios')
88
describe('controllers:book', () => {
9-
let connection
9+
let connection, user
1010

1111
beforeAll(async () => {
1212
connection = await connect()
13+
user = await getUser()
1314
})
1415

1516
afterAll(async () => {
1617
return connection.disconnect()
1718
})
1819

19-
it('should have 66 books', async () => {
20+
it('should have 8 books', async () => {
2021
const count = await Book.countDocuments()
21-
expect(count).toEqual(66)
22+
expect(count).toEqual(8)
2223
})
2324

2425
describe('getBook', () => {
2526
it('should return error 404 and "Book not found" message', async () => {
26-
const { body, statusCode } = await supertest(app).get('/books/gns')
27+
const { body, statusCode } = await supertest(app)
28+
.get('/books/app')
29+
.set('Authorization', `Bearer ${user.token}`)
2730
expect(statusCode).toBe(404)
2831
expect(body.msg).toEqual('Book not found')
2932
})
3033

31-
it('should return object named "Juízes"', async () => {
32-
const { body } = await supertest(app).get('/books/jz')
33-
expect(body.name).toBe('Juízes')
34+
it('should return object named Tiago (James)', async () => {
35+
const { body } = await supertest(app)
36+
.get('/books/tg')
37+
.set('Authorization', `Bearer ${user.token}`)
38+
expect(body.name).toBe('Tiago')
3439
})
3540

36-
it('should return object named Juizes', async () => {
37-
const { body } = await supertest(app).get('/books/jud')
38-
expect(body.name).toBe('Juízes')
41+
it('should return object named Tiago (James)', async () => {
42+
const { body } = await supertest(app)
43+
.get('/books/jm')
44+
.set('Authorization', `Bearer ${user.token}`)
45+
expect(body.name).toBe('Tiago')
3946
})
4047
})
4148

4249
describe('getBooks', () => {
43-
it('should return 66 books', async () => {
44-
const { body } = await supertest(app).get('/books')
45-
expect(body.length).toBe(66)
50+
it('should return 8 books', async () => {
51+
const { body } = await supertest(app)
52+
.get('/books')
53+
.set('Authorization', `Bearer ${user.token}`)
54+
expect(body.length).toBe(8)
4655
})
4756
})
4857
})

__test__/controllers/request.test.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import supertest from 'supertest'
22

3+
import { saveRequest } from '../../controllers/request'
34
import Request from '../../models/request'
4-
import User from '../../models/user'
55
import app from '../app'
6-
import { connect, resetDatabase } from '../utils'
6+
import { connect, getUser } from '../utils'
77

88
jest.mock('axios')
99
describe('controllers:request', () => {
@@ -12,8 +12,7 @@ describe('controllers:request', () => {
1212

1313
beforeAll(async () => {
1414
connection = await connect()
15-
await resetDatabase()
16-
user = await User.findOne()
15+
user = await getUser()
1716
})
1817

1918
afterAll(async () => {
@@ -50,4 +49,13 @@ describe('controllers:request', () => {
5049
expect(body.msg).toEqual('Not authorized token')
5150
})
5251
})
52+
53+
describe('saveRequest', () => {
54+
it('should have the same amount of requests saved', async () => {
55+
const numberOfRequestsBefore = await Request.countDocuments()
56+
await saveRequest(null)
57+
const numberOfRequestsAfter = await Request.countDocuments()
58+
expect(numberOfRequestsBefore).toEqual(numberOfRequestsAfter)
59+
})
60+
})
5361
})

__test__/controllers/user.test.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import supertest from 'supertest'
55
import { generateToken } from '../../controllers/session'
66
import User from '../../models/user'
77
import app from '../app'
8-
import { connect, resetDatabase } from '../utils'
8+
import { connect, getUser } from '../utils'
99

1010
jest.mock('axios')
1111
describe('controllers:user', () => {
@@ -14,11 +14,12 @@ describe('controllers:user', () => {
1414

1515
beforeAll(async () => {
1616
connection = await connect()
17-
await resetDatabase()
18-
user = await User.findOne()
17+
await User.deleteMany({ email: { $in: [/^fake/i] } })
18+
user = await getUser()
1919
})
2020

2121
afterAll(async () => {
22+
await User.deleteMany({ email: { $in: [/^fake/i] } })
2223
return connection.disconnect()
2324
})
2425

__test__/controllers/verse.test.js

Lines changed: 69 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,92 +2,136 @@ import supertest from 'supertest'
22

33
import Verse from '../../models/verse'
44
import app from '../app'
5-
import { connect } from '../utils'
5+
import { connect, getUser } from '../utils'
66

77
jest.mock('axios')
88
describe('controllers:verse', () => {
9-
let connection
9+
let connection, user
1010

1111
beforeAll(async () => {
1212
connection = await connect()
13+
user = await getUser()
1314
})
1415

1516
afterAll(async () => {
1617
return connection.disconnect()
1718
})
1819

19-
it('should have 31.105 verses', async done => {
20+
it('should have 837 verses', async done => {
2021
const count = await Verse.countDocuments()
21-
expect(count).toEqual(31105)
22+
expect(count).toEqual(837)
2223
done()
2324
})
2425

2526
describe('getChapter', () => {
26-
it('should return object with 6 verses and with the book of Salmos', async () => {
27-
const { body } = await supertest(app).get('/verses/nvi/sl/23')
28-
expect(body.verses.length).toBe(6)
29-
expect(body.book.name).toBe('Salmos')
27+
it('should return object with 27 verses and with the book of Tiago (James)', async () => {
28+
const { body } = await supertest(app)
29+
.get('/verses/nvi/tg/1')
30+
.set('Authorization', `Bearer ${user.token}`)
31+
expect(body.verses.length).toBe(27)
32+
expect(body.book.name).toBe('Tiago')
33+
})
34+
35+
it('should return object with 27 verses and with the book of James (Tiago)', async () => {
36+
const { body } = await supertest(app)
37+
.get('/verses/nvi/jm/1')
38+
.set('Authorization', `Bearer ${user.token}`)
39+
expect(body.verses.length).toBe(27)
40+
expect(body.book.name).toBe('Tiago')
3041
})
3142

3243
it('should return error 404 and "Book not found" message', async () => {
33-
const { body, statusCode } = await supertest(app).get('/verses/nvi/fake/23')
44+
const { body, statusCode } = await supertest(app)
45+
.get('/verses/nvi/fake/1')
46+
.set('Authorization', `Bearer ${user.token}`)
3447
expect(statusCode).toBe(404)
3548
expect(body.msg).toBe('Book not found')
3649
})
3750

3851
it('should return error 404 and "Chapter not found" message', async () => {
39-
const { body, statusCode } = await supertest(app).get('/verses/nvi/sl/160')
52+
const { body, statusCode } = await supertest(app)
53+
.get('/verses/nvi/tg/10')
54+
.set('Authorization', `Bearer ${user.token}`)
4055
expect(statusCode).toBe(404)
4156
expect(body.msg).toBe('Chapter not found')
4257
})
4358
})
4459

4560
describe('getVerse', () => {
4661
it('should return error 404 and "Verse not found" message', async () => {
47-
const { body, statusCode } = await supertest(app).get('/verses/nvi/sl/23/50')
62+
const { body, statusCode } = await supertest(app)
63+
.get('/verses/nvi/tg/1/100')
64+
.set('Authorization', `Bearer ${user.token}`)
4865
expect(statusCode).toBe(404)
4966
expect(body.msg).toBe('Verse not found')
5067
})
5168

5269
it('should return error 404 and "Book not found" message', async () => {
53-
const { body, statusCode } = await supertest(app).get('/verses/nvi/fake/23/1')
70+
const { body, statusCode } = await supertest(app)
71+
.get('/verses/nvi/fake/23/1')
72+
.set('Authorization', `Bearer ${user.token}`)
5473
expect(statusCode).toBe(404)
5574
expect(body.msg).toBe('Book not found')
5675
})
5776

58-
it('should return object with text and with the book of Mateus', async () => {
59-
const { body } = await supertest(app).get('/verses/nvi/mt/28/19')
77+
it('should return object with text and with the book of Tiago', async () => {
78+
const { body } = await supertest(app)
79+
.get('/verses/nvi/tg/1/1')
80+
.set('Authorization', `Bearer ${user.token}`)
81+
expect(body.text).toBe(
82+
'Tiago, servo de Deus e do Senhor Jesus Cristo, às doze tribos dispersas entre as nações: Saudações.'
83+
)
84+
expect(body.book.name).toBe('Tiago')
85+
})
86+
87+
it('should return object with text and with the book of James', async () => {
88+
const { body } = await supertest(app)
89+
.get('/verses/nvi/jm/1/1')
90+
.set('Authorization', `Bearer ${user.token}`)
6091
expect(body.text).toBe(
61-
'Portanto, vão e façam discípulos de todas as nações, batizando-os em nome do Pai e do Filho e do Espírito Santo,'
92+
'Tiago, servo de Deus e do Senhor Jesus Cristo, às doze tribos dispersas entre as nações: Saudações.'
6293
)
63-
expect(body.book.name).toBe('Mateus')
94+
expect(body.book.name).toBe('Tiago')
95+
})
96+
})
97+
98+
describe('getRandomVerse', () => {
99+
it('should return object with 1 verse', async () => {
100+
const { body } = await supertest(app)
101+
.get('/verses/nvi/random')
102+
.set('Authorization', `Bearer ${user.token}`)
103+
expect(body.text.length > 0).toBeTruthy()
64104
})
65105
})
66106

67107
describe('search', () => {
68108
it('should return error 404 and "Version not found" message', async () => {
69-
const { body, statusCode } = await supertest(app).post('/verses/search').send({ search: 'No princípio' })
109+
const { body, statusCode } = await supertest(app).post('/verses/search')
110+
.send({ search: 'No princípio' })
111+
.set('Authorization', `Bearer ${user.token}`)
70112
expect(statusCode).toBe(404)
71113
expect(body.msg).toBe('Version not found')
72114
})
73115

74-
it('should return 5 occurences', async () => {
116+
it('should return 16 occurences', async () => {
75117
const { body } = await supertest(app)
76118
.post('/verses/search')
77-
.send({ version: 'nvi', search: 'No princípio' })
78-
expect(body.occurrence).toBe(5)
119+
.send({ version: 'nvi', search: 'luz' })
120+
.set('Authorization', `Bearer ${user.token}`)
121+
expect(body.occurrence).toBe(16)
79122
expect(body.verses[0].text).toBe(
80-
'No princípio Deus criou os céus e a terra.'
123+
'Então a cobiça, tendo engravidado, dá à luz o pecado; e o pecado, após ter-se consumado, gera a morte.'
81124
)
82125
})
83126

84-
it('should return 5 occurences - deprecated', async () => {
127+
it('should return 16 occurences - deprecated', async () => {
85128
const { body } = await supertest(app)
86129
.post('/search')
87-
.send({ version: 'nvi', search: 'No princípio' })
88-
expect(body.occurrence).toBe(5)
130+
.send({ version: 'nvi', search: 'luz' })
131+
.set('Authorization', `Bearer ${user.token}`)
132+
expect(body.occurrence).toBe(16)
89133
expect(body.verses[0].text).toBe(
90-
'No princípio Deus criou os céus e a terra.'
134+
'Então a cobiça, tendo engravidado, dá à luz o pecado; e o pecado, após ter-se consumado, gera a morte.'
91135
)
92136
})
93137

__test__/helpers/index.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { randomNumber } from '../../helpers/'
2+
3+
jest.mock('axios')
4+
describe('helpers:randomNumber', () => {
5+
it('must return a number less than 5', async () => {
6+
const response = randomNumber(5)
7+
expect(response < 5).toBeTruthy()
8+
})
9+
10+
it('must be greater than 0', async () => {
11+
const response = randomNumber(0)
12+
expect(response > 0).toBeTruthy()
13+
})
14+
})

__test__/initDatabase.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import mongoose from 'mongoose'
2+
3+
import Book from '../models/book'
4+
import Request from '../models/request'
5+
import User from '../models/user'
6+
import Verse from '../models/verse'
7+
import books from './mock/books'
8+
import verses from './mock/verses'
9+
10+
export const connect = async () => {
11+
return mongoose.connect('mongodb://localhost/bibleapi_test', {
12+
useNewUrlParser: true,
13+
useCreateIndex: true,
14+
useUnifiedTopology: true
15+
})
16+
}
17+
18+
const createBooks = async () => {
19+
const promises = books.map(async book => new Book(book).save())
20+
return Promise.all(promises)
21+
}
22+
23+
const createVerses = async (books) => {
24+
const bookByKey = {}
25+
await books.map(book => {
26+
bookByKey[book.abbrev.en] = book
27+
})
28+
29+
const promises = verses.map(async verse => {
30+
verse.book = {
31+
id: bookByKey[verse.book.abbrev.en]._id,
32+
abbrev: bookByKey[verse.book.abbrev.en].abbrev
33+
}
34+
return new Verse(verse).save()
35+
})
36+
return Promise.all(promises)
37+
}
38+
39+
const createUser = async () => {
40+
return new User({
41+
token: '1234567890',
42+
name: 'Fake User',
43+
email: 'root@bibleapi.co',
44+
password: '123456',
45+
notifications: false
46+
}).save()
47+
}
48+
49+
export const initDatabase = async () => {
50+
const connection = await connect()
51+
await User.deleteMany()
52+
await Book.deleteMany()
53+
await Verse.deleteMany()
54+
await Request.deleteMany()
55+
const books = await createBooks()
56+
await createVerses(books)
57+
await createUser()
58+
connection.disconnect()
59+
}
60+
61+
initDatabase()

__test__/mock/books.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[{"abbrev":{"pt":"tg","en":"jm"},"name":"Tiago","chapters":5},{"abbrev":{"pt":"1pe","en":"1pe"},"name":"1 Pedro","chapters":5},{"abbrev":{"pt":"2pe","en":"2pe"},"name":"2 Pedro","chapters":3},{"abbrev":{"pt":"1jo","en":"1jo"},"name":"1 João","chapters":5},{"abbrev":{"pt":"2jo","en":"2jo"},"name":"2 João","chapters":1},{"abbrev":{"pt":"3jo","en":"3jo"},"name":"3 João","chapters":1},{"abbrev":{"pt":"jd","en":"jd"},"name":"Judas","chapters":1},{"abbrev":{"pt":"ap","en":"re"},"name":"Apocalipse","chapters":22}]

__test__/mock/verses.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)