Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/momentum-mod/website into t…
Browse files Browse the repository at this point in the history
…est/users-service
  • Loading branch information
ianwillis98 committed Jan 18, 2024
2 parents 52ca66a + 872d269 commit bb79396
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 56 deletions.
8 changes: 8 additions & 0 deletions apps/backend-e2e/src/user.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,14 @@ describe('User', () => {
});
});

it('should 400 if the authenticated user is the target user', async () => {
await req.post({
url: `user/follow/${u1.id}`,
status: 400,
token: u1Token
});
});

it('should 401 when no access token is provided', () =>
req.unauthorizedTest('user/follow/1', 'post'));
});
Expand Down
139 changes: 84 additions & 55 deletions apps/backend/src/app/modules/users/users.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,44 +68,53 @@ describe('UserService', () => {
};
jest
.spyOn(usersService['steamService'], 'getSteamUserSummaryData')
.mockImplementationOnce(() => Promise.resolve(steamUserSummaryData));
.mockResolvedValueOnce(steamUserSummaryData);
await expect(
usersService.findOrCreateFromGame(BigInt(999999999))
).rejects.toThrow(BadRequestException);
});

it('should throw an error when getSteamUserSummaryData throws and error', async () => {
jest
.spyOn(usersService['steamService'], 'getSteamUserSummaryData')
.mockImplementationOnce(() => Promise.reject('failed to start'));
.mockRejectedValueOnce('failed to start');
await expect(
usersService.findOrCreateFromGame(BigInt(123456789))
).rejects.toThrow(ServiceUnavailableException);
});

it('should call findOrCreateUser', async () => {
const steamUserSummaryData: SteamUserSummaryData = {
avatar: '',
avatarfull: '',
avatarhash: '',
avatarmedium: '',
avatar: 'avatar',
avatarfull: 'avatar full',
avatarhash: 'avatar hash',
avatarmedium: 'avatar medium',
communityvisibilitystate: 0,
lastlogoff: 0,
loccountrycode: '',
personaname: '',
personastate: 0,
personastateflags: 0,
primaryclanid: '',
profilestate: 0,
profileurl: '',
realname: '',
timecreated: 0,
steamid: '123456789'
lastlogoff: 1567,
loccountrycode: 'loc country code',
personaname: 'persona name',
personastate: 6534,
personastateflags: 343,
primaryclanid: 'primary clan id',
profilestate: 1,
profileurl: 'profile url',
realname: 'real name',
steamid: '123456789',
timecreated: 999887
};
jest
.spyOn(usersService['steamService'], 'getSteamUserSummaryData')
.mockImplementationOnce(() => Promise.resolve(steamUserSummaryData));
.mockResolvedValueOnce(steamUserSummaryData);
const spy = jest.spyOn(usersService, 'findOrCreateUser');
await usersService.findOrCreateFromGame(BigInt(123456789));
expect(spy).toHaveBeenCalled();
await usersService.findOrCreateFromGame(
BigInt(steamUserSummaryData.steamid)
);
expect(spy).toHaveBeenCalledWith({
steamID: BigInt(steamUserSummaryData.steamid),
alias: steamUserSummaryData.personaname,
avatar: steamUserSummaryData.avatarhash,
country: steamUserSummaryData.loccountrycode
});
});
});

Expand All @@ -132,6 +141,7 @@ describe('UserService', () => {
})
).rejects.toThrow(ForbiddenException);
});

it('should error when steamid is deleted', async () => {
db.deletedSteamID.findUnique.mockResolvedValueOnce({
steamID: BigInt(123456789)
Expand All @@ -157,38 +167,43 @@ describe('UserService', () => {
})
).rejects.toThrow(ForbiddenException);
});

it('should call findOrCreateUser', async () => {
const spy = jest.spyOn(usersService, 'findOrCreateUser');
spy.mockImplementationOnce(() =>
Promise.resolve({
id: 0,
steamID: 0n
})
);
await usersService.findOrCreateFromWeb({
avatar: '',
avatarfull: '',
avatarhash: '',
avatarmedium: '',
spy.mockResolvedValueOnce({
id: 0,
steamID: 0n
});
const steamUserSummaryData: SteamUserSummaryData = {
avatar: 'avatar',
avatarfull: 'avatar full',
avatarhash: 'avatar hash',
avatarmedium: 'avatar medium',
communityvisibilitystate: 0,
lastlogoff: 0,
loccountrycode: '',
personaname: '',
personastate: 0,
personastateflags: 0,
primaryclanid: '',
lastlogoff: 1567,
loccountrycode: 'loc country code',
personaname: 'persona name',
personastate: 6534,
personastateflags: 343,
primaryclanid: 'primary clan id',
profilestate: 1,
profileurl: '',
realname: '',
profileurl: 'profile url',
realname: 'real name',
steamid: '123456789',
timecreated: 0
timecreated: 999887
};
await usersService.findOrCreateFromWeb(steamUserSummaryData);
expect(spy).toHaveBeenCalledWith({
steamID: BigInt(steamUserSummaryData.steamid),
alias: steamUserSummaryData.personaname,
avatar: steamUserSummaryData.avatarhash,
country: steamUserSummaryData.loccountrycode
});
expect(spy).toHaveBeenCalled();
});

it('should error when no user is returned by findOrCreateUser', async () => {
const spy = jest.spyOn(usersService, 'findOrCreateUser');
spy.mockImplementationOnce(() => Promise.resolve(undefined));
spy.mockResolvedValueOnce(undefined);
await expect(
usersService.findOrCreateFromWeb({
avatar: '',
Expand All @@ -215,7 +230,7 @@ describe('UserService', () => {
describe('findOrCreateUser', () => {
it('should return an existing user', async () => {
const existingUser: User = {
id: 0,
id: 1234,
roles: 0,
bans: 0,
steamID: BigInt(123456789),
Expand All @@ -226,7 +241,7 @@ describe('UserService', () => {
};
db.user.findUnique.mockResolvedValueOnce(existingUser as any);
const authUser: AuthenticatedUser = {
id: 0,
id: 1234,
steamID: 123456789n
};
db.user.update.mockResolvedValueOnce(authUser as any);
Expand All @@ -239,8 +254,17 @@ describe('UserService', () => {
};
const spy = jest.spyOn(db['user'], 'update');
await usersService.findOrCreateUser(userData);
expect(spy).toHaveBeenCalled();
expect(spy).toHaveBeenCalledWith({
where: { id: existingUser.id },
data: {
alias: userData.alias,
avatar: userData.avatar.replace('_full.jpg', ''),
country: userData.country
},
select: { id: true, steamID: true }
});
});

it('should create a new user', async () => {
const nonExistingUser: User = undefined;
db.user.findUnique.mockResolvedValueOnce(nonExistingUser as any);
Expand All @@ -249,28 +273,33 @@ describe('UserService', () => {

const userData = {
steamID: BigInt(123456789),
alias: '',
avatar: '',
country: ''
alias: 'alias',
avatar: 'avatar',
country: 'country'
};
await usersService.findOrCreateUser(userData);

expect(spy).toHaveBeenCalled();
expect(spy).toHaveBeenCalledWith({
data: {
alias: userData.alias,
avatar: userData.avatar.replace('_full.jpg', ''),
country: userData.country,
steamID: userData.steamID
},
select: { id: true, steamID: true }
});
});

it('should throw error when using a limited account if the setting is on', async () => {
const nonExistingUser: User = undefined;
db.user.findUnique.mockResolvedValueOnce(nonExistingUser as any);

jest
.spyOn(usersService['config'], 'getOrThrow')
.mockImplementationOnce(() => {
return true;
});
.mockReturnValueOnce(true);
jest
.spyOn(usersService['steamService'], 'isAccountLimited')
.mockImplementationOnce(() => {
return Promise.resolve(true);
});
.mockResolvedValueOnce(true);

const userData = {
steamID: BigInt(123456789),
Expand Down
3 changes: 3 additions & 0 deletions apps/backend/src/app/modules/users/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,9 @@ export class UsersService {
if (!(await this.db.user.exists({ where: { id: targetUserID } })))
throw new NotFoundException('Target user not found');

if (localUserID === targetUserID)
throw new BadRequestException('Target user cannot be logged in user');

const isFollowing = await this.getFollower(localUserID, targetUserID);
if (isFollowing)
throw new BadRequestException(
Expand Down
2 changes: 1 addition & 1 deletion apps/frontend/src/app/pages/profile/profile.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export class ProfileComponent implements OnInit, OnDestroy {
});
}
}
this.isLocal = false;
this.isLocal = true;
this.localUserService.refreshLocalUser();
return this.localUserService.localUserSubject;
}),
Expand Down

0 comments on commit bb79396

Please sign in to comment.