Skip to content

Commit 9a0276e

Browse files
committed
test: improve coverage for service/routes/auth and fix bug
1 parent 88fd895 commit 9a0276e

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

src/service/routes/auth.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,24 +109,28 @@ router.get('/profile', async (req, res) => {
109109
router.post('/gitAccount', async (req, res) => {
110110
if (req.user) {
111111
try {
112-
let login =
112+
let username =
113113
req.body.username == null || req.body.username == 'undefined'
114114
? req.body.id
115115
: req.body.username;
116+
username = username?.split('@')[0];
116117

117-
login = login.split('@')[0];
118+
if (!username) {
119+
res.status(400).send('Error: Missing username. Git account not updated').end();
120+
return;
121+
}
118122

119-
const user = await db.findUser(login);
123+
const user = await db.findUser(username);
120124

121125
console.log('Adding gitAccount' + req.body.gitAccount);
122126
user.gitAccount = req.body.gitAccount;
123127
db.updateUser(user);
124128
res.status(200).end();
125-
} catch {
129+
} catch (e) {
126130
res
127131
.status(500)
128132
.send({
129-
message: 'An error occurred',
133+
message: `Error updating git account: ${e.message}`,
130134
})
131135
.end();
132136
}

test/testLogin.test.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,27 @@ describe('auth', async () => {
5252
res.should.have.status(200);
5353
});
5454

55+
it('should be able to set the git account', async function () {
56+
console.log(`cookie: ${cookie}`);
57+
const res = await chai.request(app).post('/api/auth/gitAccount')
58+
.set('Cookie', `${cookie}`)
59+
.send({
60+
username: 'admin',
61+
gitAccount: 'new-account',
62+
});
63+
res.should.have.status(200);
64+
});
65+
66+
it('should throw an error if the username is not provided when setting the git account', async function () {
67+
const res = await chai.request(app).post('/api/auth/gitAccount')
68+
.set('Cookie', `${cookie}`)
69+
.send({
70+
gitAccount: 'new-account',
71+
});
72+
console.log(`res: ${JSON.stringify(res)}`);
73+
res.should.have.status(400);
74+
});
75+
5576
it('should now be able to logout', async function () {
5677
const res = await chai.request(app).post('/api/auth/logout').set('Cookie', `${cookie}`);
5778
res.should.have.status(200);
@@ -78,6 +99,19 @@ describe('auth', async () => {
7899
});
79100
res.should.have.status(401);
80101
});
102+
103+
it('should fail to set the git account if the user is not logged in', async function () {
104+
const res = await chai.request(app).post('/api/auth/gitAccount').send({
105+
username: 'admin',
106+
gitAccount: 'new-account',
107+
});
108+
res.should.have.status(401);
109+
});
110+
111+
it('should fail to get the current user metadata if not logged in', async function () {
112+
const res = await chai.request(app).get('/api/auth/me');
113+
res.should.have.status(401);
114+
});
81115
});
82116

83117
after(async function () {

0 commit comments

Comments
 (0)