Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create unlink hive account endpoint #32

Open
wants to merge 1 commit into
base: align-with-lagacy-db
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion src/services/api/api.contoller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,16 @@ import { HiveModule } from '../hive/hive.module';
import { PrivateKey } from '@hiveio/dhive';
import { UserAccountModule } from '../../repositories/userAccount/user-account.module';
import { SessionModule } from '../../repositories/session/session.module';
import { HiveService } from '../hive/hive.service';

describe('ApiController', () => {
let app: INestApplication;
let mongod: MongoMemoryServer;
let authService: AuthService;
let hiveRepository: HiveChainRepository;
let emailService: EmailService;
let hiveService: HiveService;
let hiveAccountRepository: LegacyHiveAccountRepository;

beforeEach(async () => {
mongod = await MongoMemoryServer.create();
Expand Down Expand Up @@ -93,6 +96,8 @@ describe('ApiController', () => {
.compile();

authService = moduleRef.get<AuthService>(AuthService);
hiveService = moduleRef.get<HiveService>(HiveService);
hiveAccountRepository = moduleRef.get<LegacyHiveAccountRepository>(LegacyHiveAccountRepository);
hiveRepository = moduleRef.get<HiveChainRepository>(HiveChainRepository);
emailService = moduleRef.get<EmailService>(EmailService);

Expand Down Expand Up @@ -154,7 +159,7 @@ describe('ApiController', () => {
.post('/v1/hive/linkaccount')
.set('Authorization', `Bearer ${jwtToken}`)
.send(body)
//.expect(201)
.expect(201)
.then(response => {
expect(response.body).toEqual({
__v: 0,
Expand All @@ -166,6 +171,28 @@ describe('ApiController', () => {
});
});

describe('/POST /v1/hive/unlinkaccount', () => {
it('should link a Hive account', async () => {
const jwtToken = 'test_jwt_token';

const user_id = 'test_user_id'
const user = await authService.createDidUser('bob', user_id)
const hiveUsername = 'starkerz'
await hiveAccountRepository.insertCreated({ user_id: user._id, account: hiveUsername })

const body = { username: hiveUsername };

return request(app.getHttpServer())
.post('/v1/hive/unlinkaccount')
.set('Authorization', `Bearer ${jwtToken}`)
.send(body)
.expect(201)
.then(async response => {
expect(await hiveService.isHiveAccountLinked({user_id: user._id, account: hiveUsername})).toBeFalsy()
});
});
});

describe('/GET /v1/profile', () => {
it('should get the user profile', async () => {
const jwtToken = 'test_jwt_token';
Expand Down
48 changes: 48 additions & 0 deletions src/services/api/api.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { VotePostDto } from './dto/VotePost.dto';
import { parseAndValidateRequest } from '../auth/auth.utils';
import { HiveService } from '../hive/hive.service';
import { HiveChainRepository } from '../../repositories/hive-chain/hive-chain.repository';
import { UnlinkAccountPostDto } from './dto/UnlinkAccountPost.dto';

@Controller('/v1')
export class ApiController {
Expand Down Expand Up @@ -161,6 +162,53 @@ export class ApiController {
});
}

@ApiHeader({
name: 'Authorization',
description: 'JWT Authorization',
required: true,
schema: {
example:
'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c',
// default: "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c",
},
})
@ApiBody({
schema: {
properties: {
username: {
type: 'string',
default: 'test-account',
},
},
},
})
@ApiBadRequestResponse({
schema: {
properties: {
reason: {
type: 'string',
enum: ['User not found'],
default: 'User not found',
},
},
},
})
@UseGuards(AuthGuard('jwt'))
@UseInterceptors(UserDetailsInterceptor)
@Post(`/hive/unlinkaccount`)
async unlinkAccount(@Body() data: UnlinkAccountPostDto, @Request() req: unknown) {
const parsedRequest = parseAndValidateRequest(req, this.#logger);

const user = await this.authService.getUserByUserId({ user_id: parsedRequest.user.user_id });

if (!user) throw new UnauthorizedException('User not found');

return await this.authService.unlinkHiveAccount({
username: data.username,
user_id: user._id,
});
}

@ApiHeader({
name: 'Authorization',
description: 'JWT Authorization',
Expand Down
11 changes: 11 additions & 0 deletions src/services/api/dto/UnlinkAccountPost.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsNotEmpty } from 'class-validator';

export class UnlinkAccountPostDto {
@IsNotEmpty()
@ApiProperty({
description: 'Hive username for the account being unlinked',
type: 'string',
})
username: string;
}