Commit 70638bdb authored by Rosanny Sihombing's avatar Rosanny Sihombing
Browse files

update unit test

parent 908416f6
const gitlab = require('../routes/gitlab')
const axios = require('axios')
//const axios = require('axios')
//jest.mock('axios')
beforeAll(() => {
// test gitlab API connection
var config = {
method: 'get',
url: 'https://transfer.hft-stuttgart.de/gitlab/api/v4/projects'
}
axios(config)
.then(function (response) {
expect(response.statusCode).toBe(200)
describe('GitLab API', () => {
/*
test('jest.fn recalls what it has been called with', () => {
const mock = jest.fn()
mock('a', 'b', 'c')
expect(mock).toHaveBeenCalledTimes(1)
expect(mock).toHaveBeenCalledWith('a', 'b', 'c')
}); */
// mock
/* it('returns an existing gitlab-userID by an email address', done => {
let resp = {
error: false,
data: 1}
axios.get.mockResolvedValue(resp)
gitlab.getUserIdByEmail('rosanny.sihombing@hft-stuttgart.de', function(resp){
try {
expect(resp.error).toBeFalsy()
expect(resp.data).not.toBeNull()
done()
} catch (error) {
done(error)
}
})
})
*/
it('returns an existing gitlab-userID by an email address', done => {
gitlab.getUserIdByEmail('rosanny.sihombing@hft-stuttgart.de', function(data){
try {
expect(data.error).toBeFalsy()
expect(data.data).not.toBeNull()
done()
} catch (error) {
done(error)
}
})
})
it('returns an error due to the non-exist user', done => {
gitlab.getUserIdByEmail('test@hft-stuttgart.com', function (data) {
try {
expect(data.error).toBeTruthy()
done()
} catch (error) {
done(error)
}
})
})
})
describe('GitLab API test', () => {
test("Get a gitlab userID by email", () => { // email = any email address
gitlab.getUserIdByEmail("test@email.de", function(resp){
expect(resp).not.toBeNull()
})
it('returns the projects of a particular userId', done => {
gitlab.getUserProjects(3, function (data) {
try {
expect(data.error).toBeFalsy()
expect(data.data).not.toBeNull()
done()
} catch (error) {
done(error)
}
})
})
test("Get the projects of a particular user", () => { // userID = any integer
gitlab.getUserProjects(3, function(resp){
expect(resp).not.toBeNull()
})
it('returns an error due to the wrong userID', done => {
gitlab.getUserProjects('abc', function (data) {
try {
expect(data.error).toBeTruthy()
done()
} catch (error) {
done(error)
}
})
})
})
\ No newline at end of file
const dbconn = require('../routes/dbconn')
const methods = require('../routes/methods')
beforeAll(() => {
// test DB connection
dbconn.user.query('SELECT 1 + 5 AS solution', function (err, rows) {
expect(err).toBeNull()
expect(rows[0].solution).toBe(6)
})
dbconn.project.query('SELECT 10 + 5 AS solution', function (err, rows) {
expect(err).toBeNull()
expect(rows[0].solution).toBe(15)
})
})
describe("DB methohds test", () => {
/*
test("Get a user from DB by email", () => {
let gitlabUserData = "to-be-defined"
methods.addGitlabUser(gitlabUserData, function(data, err){
expect(data).not.toBeNull()
expect(err).toBeNull()
})
}) */
test("Get a user from DB by email", () => { // email = any email address
methods.getUserByEmail("test@email.de", function(data, err){
expect(data).not.toBeNull()
expect(err).toBeNull()
})
it('returns a user from DB by email', done => {
methods.getUserByEmail('rosanny.sihombing@hft-stuttgart.de', function(resp, err){
try {
expect(resp).not.toBeNull()
expect(err).toBeNull()
done()
} catch (error) {
done(error)
}
})
})
test("Get a user from DB by ID", () => { // ID = any integer
methods.getUserById(100, function(data, err){
expect(data).not.toBeNull()
expect(err).toBeNull()
})
it("returns a user from DB by ID", done => {
methods.getUserById(10, function(resp, err){
try {
expect(resp).not.toBeNull()
expect(err).toBeNull()
done()
} catch (error) {
done(error)
}
})
})
test("Check user email", () => { // email = any email address
methods.checkUserEmail("test@email.de", function(err, data){
expect(data).not.toBeNull()
expect(err).toBeNull()
it("checks user email", done => {
methods.checkUserEmail("test@email.de", function(err, resp){
try {
expect(resp).not.toBeNull()
expect(err).toBeNull()
done()
} catch (error) {
done(error)
}
})
})
test("Get a user by token", () => { // token = any alphanumeric
methods.checkUserEmail("1abc0qwerty", function(err, data){
expect(data).not.toBeNull()
expect(err).toBeNull()
it("returns a user by token", done => {
methods.checkUserEmail("1abc0qwerty", function(err, resp){ // token = any alphanumeric
try {
expect(resp).not.toBeNull()
expect(err).toBeNull()
done()
} catch (error) {
done(error)
}
})
})
})
describe('Sample Test', () => {
it('should test that true === true', () => {
expect(true).toBe(true)
})
})
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment