Skip to content

Commit

Permalink
feat: validate account before save character
Browse files Browse the repository at this point in the history
  • Loading branch information
KatoakDR committed Dec 23, 2023
1 parent 79eceaa commit 7f6efd1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
26 changes: 26 additions & 0 deletions electron/main/account/__tests__/account-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,33 @@ describe('account-service', () => {
});

describe('#saveCharacter', () => {
it('should not save a character if no account is found', async () => {
try {
await accountService.saveCharacter({
gameCode: 'DR',
accountName: 'test-account',
characterName: 'test-character',
});
fail('it should throw an error');
} catch (error) {
expect(error.message).toBe(
`[ACCOUNT:SERVICE:ERROR:ACCOUNT_NOT_FOUND] test-account`
);
}
});

it('should save a character', async () => {
storeService.get.mockImplementation((key) => {
if (key === 'sge.account.test-account') {
return {
accountName: 'test-account',
accountPassword: 'test-encrypted',
};
}

return undefined;
});

await accountService.saveCharacter({
gameCode: 'DR',
accountName: 'test-account',
Expand Down
9 changes: 9 additions & 0 deletions electron/main/account/account.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,15 @@ export class AccountServiceImpl implements AccountService {

logger.info('saving character', { accountName, characterName, gameCode });

// Confirm the account exists, otherwise we have
// no credentials by which to play the character.
const account = await this.getAccount({ accountName });
if (!account) {
throw new Error(
`[ACCOUNT:SERVICE:ERROR:ACCOUNT_NOT_FOUND] ${accountName}`
);
}

const characterKey = this.getCharacterStoreKey({
characterName,
gameCode,
Expand Down

0 comments on commit 7f6efd1

Please sign in to comment.