Skip to content

Commit 4b70bed

Browse files
authored
Merge pull request #177 from WildCodeSchool/release/part-3
Release/part 3
2 parents c910b70 + 74d917d commit 4b70bed

19 files changed

+231
-61
lines changed

packages/backend/api/src/auth/post.login.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,13 @@ postLoginRouter.post('/login', async (req, res) => {
2121
const user = await db
2222
.selectFrom('users')
2323
.leftJoin('parks', 'parks.user_id', 'users.id')
24-
.selectAll('users')
25-
.select(['parks.id as parkId'])
24+
.select([
25+
'users.password_hash',
26+
'users.id',
27+
'parks.id as parkId',
28+
'users.id',
29+
'users.email',
30+
])
2631
.where('users.email', '=', email)
2732
.executeTakeFirst();
2833

@@ -34,7 +39,9 @@ postLoginRouter.post('/login', async (req, res) => {
3439
return;
3540
}
3641

37-
const isCorrectPassword = await argon2.verify(user.password_hash, password);
42+
const { password_hash: userPasswordHash, ...restUser } = user;
43+
44+
const isCorrectPassword = await argon2.verify(userPasswordHash, password);
3845

3946
if (!isCorrectPassword) {
4047
res.json({
@@ -88,7 +95,7 @@ postLoginRouter.post('/login', async (req, res) => {
8895
res.json({
8996
message: 'User logged in!',
9097
ok: true,
91-
user,
98+
user: restUser,
9299
});
93100
});
94101

packages/backend/api/src/game/creature/post.buy-creature.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -161,12 +161,11 @@ postBuyCreature.post('/buy', async (req: Request, res) => {
161161
})
162162
.executeTakeFirst();
163163

164-
const [totalCreaturesByZone, creaturesUnlockedByZone, isNextZoneUnlocked] =
165-
await Promise.all([
166-
getTotalCreaturesByZone(zoneId),
167-
getCreaturesUnlockedByZone(zoneId, parkId),
168-
getIsNextZoneUnlocked(parkId, zoneId),
169-
]);
164+
const [isNextZoneUnlocked] = await Promise.all([
165+
getTotalCreaturesByZone(zoneId),
166+
getCreaturesUnlockedByZone(zoneId, parkId),
167+
getIsNextZoneUnlocked(parkId, zoneId),
168+
]);
170169

171170
const zone = await db
172171
.selectFrom('zones')

packages/backend/api/src/index.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,13 @@ app.use(express.json());
2020

2121
app.use('/api', router);
2222

23-
app.listen(PORT, () => {
24-
// eslint-disable-next-line no-console
25-
console.log(`Server is listening on http://${HOST}:${PORT}`);
26-
});
23+
//allow to make test one by one
24+
if (process.env.NODE_ENV !== 'test') {
25+
app.listen(PORT, () => {
26+
// eslint-disable-next-line no-console
27+
console.log(`Server is listening on http://${HOST}:${PORT}`);
28+
});
29+
}
2730

2831
declare module 'Express' {
2932
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import supertest from 'supertest';
2+
import { describe, expect, it } from 'vitest';
3+
4+
import app from '..';
5+
6+
describe('post /api/auth/log', () => {
7+
//root to login ok
8+
it('root login', async () => {
9+
const email = '[email protected]';
10+
const password = '123456';
11+
12+
const res = await supertest(app)
13+
.post('/api/auth/login')
14+
.send({ email, password });
15+
16+
//check if result is ok
17+
expect(res.status).toBe(200);
18+
expect(res.body.ok).toEqual(true);
19+
//check if we not send pass in the result
20+
expect(res.body.user.password).toBeUndefined();
21+
expect(res.body.user.password_hash).toBeUndefined();
22+
});
23+
24+
//root to login KO user is BDD but passwork KO
25+
it('wrong password should return error', async () => {
26+
const email = '[email protected]';
27+
const password = '1234';
28+
29+
const res = await supertest(app)
30+
.post('/api/auth/login')
31+
.send({ email, password });
32+
33+
//check if result is ok
34+
expect(res.body).toEqual({
35+
message: 'User or password incorrect',
36+
ok: false,
37+
});
38+
});
39+
40+
//root to login KO user is not in BDD
41+
it('wrong mail should return error', async () => {
42+
const email = '[email protected]';
43+
const password = '123456';
44+
45+
const res = await supertest(app)
46+
.post('/api/auth/login')
47+
.send({ email, password });
48+
49+
//check if result is ok
50+
expect(res.body).toEqual({
51+
message: 'User or password incorrect',
52+
ok: false,
53+
});
54+
});
55+
});
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import supertest from 'supertest';
2+
import { describe, expect, it } from 'vitest';
3+
4+
import app from '..';
5+
6+
describe('post /api/game/creature/buy', () => {
7+
//allow to stock cookie
8+
const agent = supertest.agent(app);
9+
10+
//login
11+
it('should return login ok true', async () => {
12+
const email = '[email protected]';
13+
const password = '123456';
14+
15+
const res = await agent.post('/api/auth/login').send({ email, password });
16+
17+
//check if result is ok
18+
expect(res.status).toBe(200);
19+
expect(res.body.ok).toEqual(true);
20+
//check if we not send pass in the result
21+
expect(res.body.user.password).toBeUndefined();
22+
expect(res.body.user.password_hash).toBeUndefined();
23+
});
24+
25+
it('should return creature and visitor added', async () => {
26+
const name = 'toto';
27+
const zoneId = 1;
28+
29+
const res = await agent
30+
.post('/api/game/creature/buy?creatureId=2')
31+
.send({ name, zoneId });
32+
33+
expect(res.status).toBe(200);
34+
expect(res.body).toEqual({
35+
ok: true,
36+
message: 'creature and visitor added',
37+
});
38+
});
39+
});

packages/backend/api/src/tests/demo.test.ts

Lines changed: 0 additions & 13 deletions
This file was deleted.

packages/frontend/web/src/components/barrier.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type BarrierProps = {
1919
};
2020

2121
export default function Barrier({ barrier, refetch }: BarrierProps) {
22-
const { wallet } = useGameInfoContext();
22+
const { wallet, walletRefetch } = useGameInfoContext();
2323
const hasEnoughMoons = wallet >= barrier.price;
2424
const priceFormatted = formatNumber(barrier.price);
2525

@@ -41,6 +41,7 @@ export default function Barrier({ barrier, refetch }: BarrierProps) {
4141

4242
if (result.ok === true) {
4343
await refetch();
44+
await walletRefetch();
4445
}
4546
} catch (error) {
4647
// eslint-disable-next-line no-console

packages/frontend/web/src/components/buy-creature.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export default function BuyCreature({
2828
creaturesRefetch,
2929
visitorsRefetch,
3030
parkRefetch,
31+
walletRefetch,
3132
creaturesEnclos,
3233
} = useGameInfoContext();
3334
const { zone_id: zoneId } = useParams();
@@ -80,6 +81,7 @@ export default function BuyCreature({
8081
fetchCreatures(),
8182
creaturesRefetch(),
8283
visitorsRefetch(),
84+
walletRefetch(),
8385
]);
8486

8587
setName('');

packages/frontend/web/src/components/creature-line.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export default function CreatureLine({
3636
creature,
3737
potionPrice,
3838
}: CreatureLineProps) {
39-
const { wallet, parkRefetch } = useGameInfoContext();
39+
const { wallet, parkRefetch, walletRefetch } = useGameInfoContext();
4040
const [isClicked, setIsClicked] = useState(false);
4141
const hasEnoughMoons = wallet >= Number(potionPrice);
4242

@@ -57,7 +57,7 @@ export default function CreatureLine({
5757

5858
const result = await response.json();
5959
if (result.ok === true) {
60-
await Promise.all([fetchCreatures(), parkRefetch()]);
60+
await Promise.all([fetchCreatures(), parkRefetch(), walletRefetch()]);
6161
}
6262
} catch (error) {
6363
// eslint-disable-next-line no-console

packages/frontend/web/src/components/enclosure.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useState } from 'react';
1+
import { useEffect, useState } from 'react';
22

33
import type { Decorations } from '@app/api';
44
import type { Enclosure } from '@app/api';
@@ -46,6 +46,16 @@ export default function Enclosure({
4646

4747
const sizeEnclos = isFour ? 'w-1/2' : isSix ? 'w-1/3' : '';
4848

49+
useEffect(() => {
50+
const interval = setInterval(async () => {
51+
await refetchCreature();
52+
}, 5000);
53+
54+
return () => {
55+
clearInterval(interval);
56+
};
57+
}, [refetchCreature]);
58+
4959
return (
5060
<div
5161
className={`relative flex h-[50vh] ${sizeEnclos} cursor-pointer flex-col justify-center p-4 ${getBackgroundEnclosure(enclosures.background)} `}

0 commit comments

Comments
 (0)