Skip to content

Commit

Permalink
fix(typeorm): fix query in findUserByServiceId accounts-js#1108 (acco…
Browse files Browse the repository at this point in the history
  • Loading branch information
blessanabraham authored Feb 5, 2021
1 parent 232de32 commit 2450f33
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 55 deletions.
99 changes: 47 additions & 52 deletions packages/database-typeorm/__tests__/typeorm-password.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,14 +193,13 @@ describe('TypeormServicePassword', () => {
it('should return user', async () => {
const accountsTypeorm = new AccountsTypeorm({ connection, cache: 1 });
const userId = await accountsTypeorm.createUser(user);
const ret = await accountsTypeorm.findUserById(userId);
const ret1 = await accountsTypeorm.findUserByServiceId(
'password',
ret!.services.password[0].id
);

expect(ret1).toBeTruthy();
expect(ret1!.id).toEqual(ret!.id);
let ret = await accountsTypeorm.findUserByServiceId('facebook', '1');
expect(ret).not.toBeTruthy();
await accountsTypeorm.setService(userId, 'facebook', { id: '1' });
ret = await accountsTypeorm.findUserByServiceId('facebook', '1');
expect(ret).toBeTruthy();
expect(ret!.id).toEqual(userId);
});
});

Expand Down Expand Up @@ -238,26 +237,24 @@ describe('TypeormServicePassword', () => {
});
});

// TODO: Uncomment this once #1108 is fixed
// describe('findUserByServiceId', () => {
// it('should return null for not found user', async () => {
// const accountsTypeorm = new AccountsTypeorm({ connection });
// const ret = await accountsTypeorm.findUserByServiceId('facebook', 'invalid');
// expect(ret).not.toBeTruthy();
// });
//
// it('should return user', async () => {
// const accountsTypeorm = new AccountsTypeorm({ connection });
// const userId = await accountsTypeorm.createUser(user);
// let ret = await accountsTypeorm.findUserByServiceId('facebook', '1');
// expect(ret).not.toBeTruthy();
// await accountsTypeorm.setService(userId, 'facebook', { id: '1' });
// ret = await accountsTypeorm.findUserByServiceId('facebook', '1');
// expect(ret).toBeTruthy();
// expect((ret as any)._id).toBeTruthy();
// expect(ret!.id).toBeTruthy();
// });
// });
describe('findUserByServiceId', () => {
it('should return null for not found user', async () => {
const accountsTypeorm = new AccountsTypeorm({ connection });
const ret = await accountsTypeorm.findUserByServiceId('facebook', 'invalid');
expect(ret).not.toBeTruthy();
});

it('should return user', async () => {
const accountsTypeorm = new AccountsTypeorm({ connection });
const userId = await accountsTypeorm.createUser(user);
let ret = await accountsTypeorm.findUserByServiceId('facebook', '1');
expect(ret).not.toBeTruthy();
await accountsTypeorm.setService(userId, 'facebook', { id: '1' });
ret = await accountsTypeorm.findUserByServiceId('facebook', '1');
expect(ret).toBeTruthy();
expect(ret!.id).toBeTruthy();
});
});

describe('findPasswordHash', () => {
it('should return null on not found user', async () => {
Expand Down Expand Up @@ -418,31 +415,29 @@ describe('TypeormServicePassword', () => {
});
});

// TODO: The query in findUserByServiceId is wrong and hence this will fail
// describe('setService', () => {
// it('should set service', async () => {
// const accountsTypeorm = new AccountsTypeorm({ connection });
// const userId = await accountsTypeorm.createUser(user);
// let ret = await accountsTypeorm.findUserByServiceId('google', '1');
// expect(ret).not.toBeTruthy();
// await accountsTypeorm.setService(userId, 'google', { id: 1 });
// ret = await accountsTypeorm.findUserByServiceId('google', '1');
// expect(ret).toBeTruthy();
// expect(ret!.id).toBeTruthy();
// });
// });

// TODO: The query in findUserByServiceId is wrong and hence this will fail
// describe('unsetService', () => {
// it('should unset service', async () => {
// const accountsTypeorm = new AccountsTypeorm({ connection });
// const userId = await accountsTypeorm.createUser(user);
// await accountsTypeorm.setService(userId, 'telegram', { id: '1' });
// await accountsTypeorm.unsetService(userId, 'telegram');
// const ret = await accountsTypeorm.findUserByServiceId('telegram', '1');
// expect(ret).not.toBeTruthy();
// });
// });
describe('setService', () => {
it('should set service', async () => {
const accountsTypeorm = new AccountsTypeorm({ connection });
const userId = await accountsTypeorm.createUser(user);
let ret = await accountsTypeorm.findUserByServiceId('google', '1');
expect(ret).not.toBeTruthy();
await accountsTypeorm.setService(userId, 'google', { id: 1 });
ret = await accountsTypeorm.findUserByServiceId('google', '1');
expect(ret).toBeTruthy();
expect(ret!.id).toBeTruthy();
});
});

describe('unsetService', () => {
it('should unset service', async () => {
const accountsTypeorm = new AccountsTypeorm({ connection });
const userId = await accountsTypeorm.createUser(user);
await accountsTypeorm.setService(userId, 'telegram', { id: '1' });
await accountsTypeorm.unsetService(userId, 'telegram');
const ret = await accountsTypeorm.findUserByServiceId('telegram', '1');
expect(ret).not.toBeTruthy();
});
});

describe('setUserDeactivated', () => {
// TODO: This should be added
Expand Down
8 changes: 5 additions & 3 deletions packages/database-typeorm/src/typeorm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,12 @@ export class AccountsTypeorm implements DatabaseInterface {

public async findUserByServiceId(serviceName: string, serviceId: string): Promise<User | null> {
const service = await this.serviceRepository.findOne({
name: serviceName,
serviceId,
where: {
name: serviceName,
serviceId,
},
cache: this.options.cache,
} as any);
});

if (service) {
return this.findUserById(service.userId);
Expand Down

0 comments on commit 2450f33

Please sign in to comment.