Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion backend/routes/userRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const bcrypt = require('bcrypt');
// nao tenho certeza se esta certo


const UserController = require("../controllers/userController")
const UserController = require("../controllers/UserController")

router.post('/signup',UserController.user_signup)

Expand Down
78 changes: 78 additions & 0 deletions backend/tests/controllers/login/login1.steps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { loadFeature, defineFeature } from "jest-cucumber"
import axios, { AxiosResponse } from 'axios'
const bcrypt = require('bcrypt');
const mongoose = require('mongoose')
const User = require("../../../models/User")

const feature = loadFeature('tests/features/login/login1.feature');

const SERVER_URL = 'http://localhost:3001'

export async function connectDBForTesting() {
try {
const dbUri = "mongodb://localhost:27017";
const dbName = "test";
await mongoose.connect(dbUri, {
dbName,
autoCreate: true,
});
} catch (error) {
console.log("DB connect error");
}
}

export async function disconnectDBForTesting() {
try {
await mongoose.connection.close();
} catch (error) {
console.log("DB disconnect error");
}
}

defineFeature(feature, test => {

beforeAll(async () => {
await connectDBForTesting();
});
afterAll(async () => {
await disconnectDBForTesting();
});

let response: AxiosResponse

test('criar usuário', ({ given, when, then, and }) => {
given(/^não existe um usuário cadastrado com o nome "(.*)", email "(.*)" e senha "(.*)"$/, async (name,email,password) => {

const user = await User.findOne({name: name, email: email, password: password})

expect(user).toBe(
null
)

})
when(/^uma requisição POST foi enviada para "(.*)" com o nome "(.*)", email "(.*)" e senha "(.*)"$/, async (path,name,email,password) => {
try {
response = await axios.post(`${SERVER_URL}${path}`,{name:name,email:email,password:password})
} catch (error) {
console.error('Error during HTTP request:', error)
return
}
})
then(/^o status de resposta é "(.*)"$/, (status) => {
expect(String(response.status)).toBe(status)
})
and(/^um usuário é cadastrado com nome "(.*)", email "(.*)" e senha "(.*)"$/, async (name,email,password) => {
const user = await User.findOne({name: name, email: email})
if(user){
console.log(user.name)
}
const verify = await bcrypt.compare(password,user.password)
let compare = true
if(!verify){
compare = false
}
expect(compare).toBe(true)

})
});
});
76 changes: 76 additions & 0 deletions backend/tests/controllers/login/login2.steps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { loadFeature, defineFeature } from "jest-cucumber"
import axios, { AxiosResponse } from 'axios'
const bcrypt = require('bcrypt');
const mongoose = require('mongoose')
const User = require("../../../models/User")

const feature = loadFeature('tests/features/login/login2.feature');

const SERVER_URL = 'http://localhost:3001'

export async function connectDBForTesting() {
try {
const dbUri = "mongodb://localhost:27017";
const dbName = "test";
await mongoose.connect(dbUri, {
dbName,
autoCreate: true,
});
} catch (error) {
console.log("DB connect error");
}
}

export async function disconnectDBForTesting() {
try {
await mongoose.connection.close();
} catch (error) {
console.log("DB disconnect error");
}
}

defineFeature(feature, test => {

beforeAll(async () => {
await connectDBForTesting();
});
afterAll(async () => {
await disconnectDBForTesting();
});

let response: AxiosResponse

test('logar o usuário', ({ given, when, then, and }) => {
given(/^existe um usuário com nome "(.*)", email "(.*)" e senha "(.*)"$/, async (name,email,password) => {

const user = await User.findOne({name: name, email: email, password: password})

expect(user).toBe(
null
)

})
when(/^uma requisição POST foi enviada para "(.*)" com o email "(.*)" e senha "(.*)"$/, async (path,email,password) => {
try {
response = await axios.post(`${SERVER_URL}${path}`,{email:email,password:password})
} catch (error) {
console.error('Error during HTTP request:', error)
return
}
})
then(/^o status de resposta é "(.*)"$/, (status) => {
expect(String(response.status)).toBe(status)
})
and(/^um usuário é logado com nome "(.*)" e email "(.*)"$/, async (name,email,password) => {
const user = await User.findOne({name: name, email: email})

let compare = false
if(name == user.name){
compare = true
}

expect(compare).toBe(true)

})
});
});
76 changes: 76 additions & 0 deletions backend/tests/controllers/login/login3.steps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { loadFeature, defineFeature } from "jest-cucumber"
import axios, { AxiosResponse } from 'axios'
const bcrypt = require('bcrypt');
const mongoose = require('mongoose')
const User = require("../../../models/User")

const feature = loadFeature('tests/features/login/login3.feature');

const SERVER_URL = 'http://localhost:3001'

export async function connectDBForTesting() {
try {
const dbUri = "mongodb://localhost:27017";
const dbName = "test";
await mongoose.connect(dbUri, {
dbName,
autoCreate: true,
});
} catch (error) {
console.log("DB connect error");
}
}

export async function disconnectDBForTesting() {
try {
await mongoose.connection.close();
} catch (error) {
console.log("DB disconnect error");
}
}

defineFeature(feature, test => {

beforeAll(async () => {
await connectDBForTesting();
});
afterAll(async () => {
await disconnectDBForTesting();
});

let response: AxiosResponse

test('A senha não contém caracteres especiais e letras maiusculas', ({ given, when, then, and }) => {
given(/^não existe um usuário cadastrado com o nome "(.*)", email "(.*)" e senha "(.*)"$/, async (name,email,password) => {

const user = await User.findOne({name: name, email: email, password: password})

expect(user).toBe(
null
)

})
when(/^uma requisição POST foi enviada para "(.*)" com nome "(.*)", email "(.*)" e senha "(.*)"$/, async (path,name,email,password) => {
try {
response = await axios.post(`${SERVER_URL}${path}`,{name:name,email:email,password:password})
} catch (error) {

return
}
})
then(/^o status de resposta é "(.*)"$/, (status) => {
expect(response).toBe(undefined)
})
and(/^é retornado o aviso "(.*)"$/, async (msg) => {


let compare = false
if(msg == "A senha deve conter no mínimo 1 caracter maiúsculo, 1 caracter minúsculo, 1 simbolo especial e tamanho de pelo menos 8."){
compare = true
}

expect(compare).toBe(true)

})
});
});
76 changes: 76 additions & 0 deletions backend/tests/controllers/login/login4.steps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { loadFeature, defineFeature } from "jest-cucumber"
import axios, { AxiosResponse } from 'axios'
const bcrypt = require('bcrypt');
const mongoose = require('mongoose')
const User = require("../../../models/User")

const feature = loadFeature('tests/features/login/login4.feature');

const SERVER_URL = 'http://localhost:3001'

export async function connectDBForTesting() {
try {
const dbUri = "mongodb://localhost:27017";
const dbName = "test";
await mongoose.connect(dbUri, {
dbName,
autoCreate: true,
});
} catch (error) {
console.log("DB connect error");
}
}

export async function disconnectDBForTesting() {
try {
await mongoose.connection.close();
} catch (error) {
console.log("DB disconnect error");
}
}

defineFeature(feature, test => {

beforeAll(async () => {
await connectDBForTesting();
});
afterAll(async () => {
await disconnectDBForTesting();
});

let response: AxiosResponse

test('A senha para login está errada', ({ given, when, then, and }) => {
given(/^existe um usuário cadastrado com nome "(.*)", email "(.*)" e senha "(.*)"$/, async (name,email,password) => {

const user = await User.findOne({name: name, email: email, password: password})

expect(user).toBe(
null
)

})
when(/^uma requisição POST foi enviada para "(.*)" com o email "(.*)" e senha "(.*)"$/, async (path,email,password) => {
try {
response = await axios.post(`${SERVER_URL}${path}`,{email:email,password:password})
} catch (error) {

return
}
})
then(/^o status da resposta é "(.*)"$/, (status) => {
expect(response).toBe(undefined)
})
and(/^é retornado o aviso "(.*)"$/, async (msg) => {


let compare = false
if(msg == "Invalid password"){
compare = true
}

expect(compare).toBe(true)

})
});
});
10 changes: 10 additions & 0 deletions backend/tests/features/login/login1.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Feature:Login

Scenario:criar usuário
Given não existe um usuário cadastrado com o nome "jose", email "jose@gmail.com" e senha "Joao1245&"
When uma requisição POST foi enviada para "/users/signup" com o nome "jose", email "jose@gmail.com" e senha "Joao1245&"
Then o status de resposta é "200"
And um usuário é cadastrado com nome "jose", email "jose@gmail.com" e senha "Joao1245&"



7 changes: 7 additions & 0 deletions backend/tests/features/login/login2.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Feature:Login

Scenario:logar o usuário
Given existe um usuário com nome "Joao", email "joao@gmail.com" e senha "Joao1245&"
When uma requisição POST foi enviada para "/users/signin " com o email "joao@gmail.com" e senha "Joao1245&"
Then o status de resposta é "200"
And um usuário é logado com nome "Joao" e email "joao@gmail.com"
7 changes: 7 additions & 0 deletions backend/tests/features/login/login3.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Feature:Login

Scenario:A senha não contém caracteres especiais e letras maiusculas
Given não existe um usuário cadastrado com o nome "tiago", email "tiago@gmail.com" e senha "senhasimples"
When uma requisição POST foi enviada para "/users/signup" com nome "tiago", email "tiago@gmail.com" e senha "senhasimples"
Then o status de resposta é "404 Not Found"
And é retornado o aviso "A senha deve conter no mínimo 1 caracter maiúsculo, 1 caracter minúsculo, 1 simbolo especial e tamanho de pelo menos 8."
7 changes: 7 additions & 0 deletions backend/tests/features/login/login4.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Feature:Login

Scenario:A senha para login está errada
Given existe um usuário cadastrado com nome "Joao", email "joao@gmail.com" e senha "Joao1245&"
When uma requisição POST foi enviada para "/user/signin" com o email "joao@gmail.com" e senha "Joao456&"
Then o status da resposta é "404 Not Found"
And é retornado o aviso "Invalid password"