Skip to content

Commit a807c17

Browse files
Abhay5855pushpendu
andauthored
Feat : username and twitter id contains @ / special characters in the value in some user data. (#1320)
* feat:add validations in the signup api to check that username must be in the specific format and social id must not contain the special character @ to it also modify the error with a proper error message * fix:the error message description * fix:change the error message according to specific social id change * fix:change the regex validation such that now it will accept numbers or characters or combination of both * refactor:change the error message to meaningful text * feat:add tests for invalid username and valid username and invalid social id * fix:match the error message with the failing test case message, update the error messages in the signup api * fix:remove unnecessary validation for linkedin_id, intagram_id we are not sending it during signup also remove the tests for that * fix:remove unnecessary validation for linkedin_id, intagram_id we are not sending it during signup also remove the tests for that * feat:add unit testing for the update user function to check if invalid username and twitter_id is passed or not --------- Co-authored-by: pushpendu <[email protected]>
1 parent 7554970 commit a807c17

File tree

3 files changed

+138
-2
lines changed

3 files changed

+138
-2
lines changed

middlewares/validators/user.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,25 @@ const updateUser = async (req, res, next) => {
1313
.keys({
1414
phone: joi.string().optional(),
1515
email: joi.string().optional(),
16-
username: joi.string().optional(),
16+
username: joi
17+
.string()
18+
.optional()
19+
.min(4)
20+
.max(20)
21+
.regex(/^[a-zA-Z0-9]+$/)
22+
.message("Username must be between 4 and 20 characters long and contain only letters or numbers."),
1723
first_name: joi.string().optional(),
1824
last_name: joi.string().optional(),
1925
yoe: joi.number().min(0).optional(),
2026
company: joi.string().optional(),
2127
designation: joi.string().optional(),
2228
img: joi.string().optional(),
2329
linkedin_id: joi.string().optional(),
24-
twitter_id: joi.string().optional(),
30+
twitter_id: joi
31+
.string()
32+
.optional()
33+
.regex(/^[^@]*$/)
34+
.message("Invalid Twitter ID. ID should not contain special character @"),
2535
instagram_id: joi.string().optional(),
2636
website: joi.string().optional(),
2737
status: joi

test/integration/users.test.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,25 @@ describe("Users", function () {
100100
});
101101
});
102102

103+
it("Should update the username with valid username", function (done) {
104+
chai
105+
.request(app)
106+
.patch("/users/self")
107+
.set("cookie", `${cookieName}=${jwt}`)
108+
.send({
109+
username: "validUsername123",
110+
})
111+
.end((err, res) => {
112+
if (err) {
113+
return done(err);
114+
}
115+
116+
expect(res).to.have.status(204);
117+
118+
return done();
119+
});
120+
});
121+
103122
it("Should return 400 for invalid status value", function (done) {
104123
chai
105124
.request(app)
@@ -124,6 +143,56 @@ describe("Users", function () {
124143
return done();
125144
});
126145
});
146+
147+
it("Should return 400 for invalid username", function (done) {
148+
chai
149+
.request(app)
150+
.patch("/users/self")
151+
.set("cookie", `${cookieName}=${jwt}`)
152+
.send({
153+
username: "@invalidUser-name",
154+
})
155+
.end((err, res) => {
156+
if (err) {
157+
return done(err);
158+
}
159+
160+
expect(res).to.have.status(400);
161+
expect(res.body).to.be.an("object");
162+
expect(res.body).to.eql({
163+
statusCode: 400,
164+
error: "Bad Request",
165+
message: "Username must be between 4 and 20 characters long and contain only letters or numbers.",
166+
});
167+
168+
return done();
169+
});
170+
});
171+
172+
it("Should return 400 for invalid Twitter ID", function (done) {
173+
chai
174+
.request(app)
175+
.patch("/users/self")
176+
.set("cookie", `${cookieName}=${jwt}`)
177+
.send({
178+
twitter_id: "invalid@twitter_id",
179+
})
180+
.end((err, res) => {
181+
if (err) {
182+
return done(err);
183+
}
184+
185+
expect(res).to.have.status(400);
186+
expect(res.body).to.be.an("object");
187+
expect(res.body).to.eql({
188+
statusCode: 400,
189+
error: "Bad Request",
190+
message: "Invalid Twitter ID. ID should not contain special character @",
191+
});
192+
193+
return done();
194+
});
195+
});
127196
});
128197

129198
describe("GET /users", function () {

test/unit/middlewares/user-validator.test.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
const sinon = require("sinon");
22
const { validateJoinData } = require("./../../../middlewares/validators/user");
33
const joinData = require("./../../fixtures/user/join");
4+
const userData = require("./../../fixtures/user/user");
45
const { expect } = require("chai");
6+
const { updateUser } = require("./../../../middlewares/validators/user");
57

68
describe("Middleware | Validators | User", function () {
79
describe("Create user validator for validateJoinData", function () {
@@ -34,4 +36,59 @@ describe("Middleware | Validators | User", function () {
3436
expect(nextSpy.calledOnce).to.be.equal(false);
3537
});
3638
});
39+
40+
describe("Create user validator for updateUser", function () {
41+
it("lets the request pass to next", async function () {
42+
const req = {
43+
body: userData[1],
44+
};
45+
46+
const res = {};
47+
const next = sinon.spy();
48+
await updateUser(req, res, next);
49+
expect(next.calledOnce).to.be.equal(true);
50+
});
51+
52+
it("Stops the propagation of the next if twitter_id is invalid", async function () {
53+
const req = {
54+
body: {
55+
last_name: "patil",
56+
first_name: "Abhay",
57+
username: "invalidusername",
58+
twitter_id: "@abhayisawesome",
59+
},
60+
};
61+
const res = {
62+
boom: {
63+
badRequest: () => {},
64+
},
65+
};
66+
const nextSpy = sinon.spy();
67+
await updateUser(req, res, nextSpy).catch((err) => {
68+
expect(err).to.be.an.instanceOf(Error);
69+
});
70+
expect(nextSpy.calledOnce).to.be.equal(false);
71+
});
72+
73+
it("Stops the propagation of the next if username is invalid", async function () {
74+
const req = {
75+
body: {
76+
last_name: "patil",
77+
first_name: "Abhay",
78+
username: "@invalidusername-12",
79+
twitter_id: "abhayisawesome",
80+
},
81+
};
82+
const res = {
83+
boom: {
84+
badRequest: () => {},
85+
},
86+
};
87+
const nextSpy = sinon.spy();
88+
await updateUser(req, res, nextSpy).catch((err) => {
89+
expect(err).to.be.an.instanceOf(Error);
90+
});
91+
expect(nextSpy.calledOnce).to.be.equal(false);
92+
});
93+
});
3794
});

0 commit comments

Comments
 (0)