-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[delivers #187584915] Added user login
- Loading branch information
1 parent
15533e4
commit 213e89c
Showing
12 changed files
with
321 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { QueryInterface, DataTypes } from "sequelize"; | ||
export default { | ||
up: async (queryInterface: QueryInterface) => { | ||
await queryInterface.createTable("tokens", { | ||
id: { | ||
type: DataTypes.INTEGER, | ||
autoIncrement: true, | ||
primaryKey: true | ||
}, | ||
userId: { | ||
type: new DataTypes.INTEGER, | ||
allowNull: false | ||
}, | ||
device: { | ||
type: new DataTypes.STRING(280), | ||
allowNull: true | ||
}, | ||
accessToken: { | ||
type: new DataTypes.STRING(280), | ||
allowNull: true | ||
}, | ||
verifyToken: { | ||
type: new DataTypes.STRING(280), | ||
allowNull: true | ||
}, | ||
createdAt: { | ||
field: "createdAt", | ||
type: DataTypes.DATE, | ||
allowNull: false, | ||
defaultValue: DataTypes.NOW | ||
}, | ||
updatedAt: { | ||
field: "updatedAt", | ||
type: DataTypes.DATE, | ||
allowNull: false, | ||
defaultValue: DataTypes.NOW | ||
} | ||
}); | ||
}, | ||
|
||
down: async (queryInterface: QueryInterface) => { | ||
await queryInterface.dropTable("tokens"); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
/* eslint-disable require-jsdoc */ | ||
import { Model, DataTypes } from "sequelize"; | ||
import sequelizeConnection from "../config/db.config"; | ||
export interface TokenAttributes { | ||
id: number; | ||
userId: number; | ||
device: string; | ||
accessToken: string; | ||
verifyToken: string; | ||
createdAt: Date; | ||
updatedAt: Date; | ||
} | ||
|
||
class Tokens extends Model<TokenAttributes> implements TokenAttributes { | ||
declare id: number; | ||
declare userId: number; | ||
declare device: string; | ||
declare accessToken: string; | ||
declare verifyToken:string; | ||
declare createdAt: Date; | ||
declare updatedAt: Date; | ||
|
||
static associate(models: any) { | ||
Tokens.belongsTo(models.Users, { foreignKey: "userId",as: "user" }); | ||
} | ||
} | ||
|
||
Tokens.init( | ||
{ | ||
id: { | ||
type: DataTypes.INTEGER, | ||
autoIncrement: true, | ||
primaryKey: true | ||
}, | ||
userId: { | ||
type: new DataTypes.INTEGER, | ||
allowNull: false | ||
}, | ||
device: { | ||
type: new DataTypes.STRING(280), | ||
allowNull: true | ||
}, | ||
accessToken: { | ||
type: new DataTypes.STRING(280), | ||
allowNull: true | ||
}, | ||
verifyToken: { | ||
type: new DataTypes.STRING(280), | ||
allowNull: true | ||
}, | ||
createdAt: { | ||
field: "createdAt", | ||
type: DataTypes.DATE, | ||
allowNull: false, | ||
defaultValue: DataTypes.NOW | ||
}, | ||
updatedAt: { | ||
field: "updatedAt", | ||
type: DataTypes.DATE, | ||
allowNull: false, | ||
defaultValue: DataTypes.NOW | ||
} | ||
}, | ||
{ | ||
sequelize: sequelizeConnection, | ||
tableName: "tokens", | ||
timestamps: true, | ||
modelName:"Tokens" | ||
} | ||
); | ||
|
||
export default Tokens; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,11 +8,11 @@ import { isUserExist } from "../../../middlewares/validation"; | |
import authRepositories from "../repository/authRepositories"; | ||
import Users from "../../../databases/models/users"; | ||
|
||
|
||
|
||
chai.use(chaiHttp); | ||
const router = () => chai.request(app); | ||
|
||
describe("Authentication Test Cases", () => { | ||
|
||
it("Should be able to register new user", (done) => { | ||
router() | ||
.post("/api/auth/register") | ||
|
@@ -29,6 +29,66 @@ describe("Authentication Test Cases", () => { | |
}); | ||
}); | ||
|
||
it("Should be able to login a registered user", (done) => { | ||
router() | ||
.post("/api/auth/login") | ||
.send({ | ||
email: "[email protected]", | ||
password: "password123" | ||
}) | ||
.end((error, response) => { | ||
expect(response.status).to.equal(httpStatus.OK); | ||
expect(response.body).to.be.a("object"); | ||
expect(response.body).to.have.property("data"); | ||
expect(response.body.message).to.be.a("string"); | ||
expect(response.body.data).to.have.property("token"); | ||
done(error); | ||
}); | ||
}); | ||
|
||
it("Should return validation error when no email or password given", (done) => { | ||
router() | ||
.post("/api/auth/login") | ||
.send({ | ||
email: "[email protected]" | ||
}) | ||
.end((error, response) => { | ||
expect(response).to.have.status(httpStatus.BAD_REQUEST); | ||
expect(response.body).to.be.a("object"); | ||
done(error); | ||
}); | ||
}); | ||
|
||
it("Should not be able to login user with invalid Email", (done) => { | ||
router() | ||
.post("/api/auth/login") | ||
.send({ | ||
email: "[email protected]", | ||
password: "fakepassword" | ||
}) | ||
.end((error, response) => { | ||
expect(response).to.have.status(httpStatus.BAD_REQUEST); | ||
expect(response.body).to.be.a("object"); | ||
expect(response.body).to.have.property("message", "Invalid Email or Password"); | ||
done(error); | ||
}); | ||
}); | ||
|
||
it("Should not be able to login user with invalid Password", (done) => { | ||
router() | ||
.post("/api/auth/login") | ||
.send({ | ||
email: "[email protected]", | ||
password: "fakepassword" | ||
}) | ||
.end((error, response) => { | ||
expect(response).to.have.status(httpStatus.BAD_REQUEST); | ||
expect(response.body).to.be.a("object"); | ||
expect(response.body).to.have.property("message", "Invalid Email or Password"); | ||
done(error); | ||
}); | ||
}); | ||
|
||
it("should return validation return message error and 400", (done) => { | ||
router() | ||
.post("/api/auth/register") | ||
|
@@ -43,6 +103,7 @@ describe("Authentication Test Cases", () => { | |
done(error); | ||
}); | ||
}); | ||
|
||
}) | ||
|
||
describe("isUserExist Middleware", () => { | ||
|
@@ -102,6 +163,7 @@ describe("isUserExist Middleware", () => { | |
}); | ||
}); | ||
}); | ||
|
||
describe("POST /auth/register - Error Handling", () => { | ||
let registerUserStub: sinon.SinonStub; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,13 @@ | ||
import {validation,isUserExist} from "../middlewares/validation"; | ||
import { validation, isUserExist, checkLoginUser } from "../middlewares/validation"; | ||
import authControllers from "../modules/auth/controller/authControllers"; | ||
import { Router } from "express"; | ||
import {authSchema} from "../modules/auth/validation/authValidations"; | ||
import { authSchema, loginSchema } from "../modules/auth/validation/authValidations"; | ||
|
||
|
||
const router: Router = Router(); | ||
|
||
router.post("/register", validation(authSchema), isUserExist, authControllers.registerUser); | ||
router.post("/login", validation(loginSchema), checkLoginUser, authControllers.loginUser); | ||
|
||
|
||
export default router; |
Oops, something went wrong.