-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#9 - Added changes according to feedback
- Loading branch information
1 parent
76672cc
commit d63a7b8
Showing
11 changed files
with
99 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,31 @@ | ||
import { User } from '@prisma/client'; | ||
import UserService from '../../../auth/user.service'; | ||
import AuthService from '../../../auth/auth.service'; | ||
import FolderService from '../folder.service'; | ||
|
||
describe('POST /api/folder', () => { | ||
let userService: UserService; | ||
let authService: AuthService; | ||
let folderService: FolderService; | ||
|
||
let user: User; | ||
let otherUser: User; | ||
|
||
beforeAll(async () => { | ||
authService = new AuthService(); | ||
userService = new UserService(); | ||
folderService = new FolderService(); | ||
|
||
user = await userService.createUser({ | ||
name: 'Joe Biden the 1st', | ||
email: '[email protected]', | ||
password: '1234', | ||
}); | ||
otherUser = await userService.createUser({ | ||
name: 'Joe Biden the 2nd', | ||
email: '[email protected]', | ||
password: '4321', | ||
}); | ||
}); | ||
|
||
it('should return status 200 and folder', async () => { | ||
|
@@ -72,6 +81,39 @@ describe('POST /api/folder', () => { | |
}); | ||
}); | ||
|
||
it('should return status 401, when parent id is provided, but no access to parent', async () => { | ||
const { accessToken } = await authService.createTokens(user.id); | ||
|
||
const folder = await folderService.createFolder({ | ||
name: 'Folder1', | ||
ownerId: otherUser.id, | ||
parentId: null, | ||
color: '#78BC61', | ||
}); | ||
|
||
const response = await global.fastify.inject({ | ||
method: 'POST', | ||
url: '/api/folder', | ||
headers: { | ||
authorization: 'Bearer ' + accessToken, | ||
}, | ||
payload: { | ||
name: 'Folder Name', | ||
color: '#78BC61', | ||
parentId: folder.id, | ||
}, | ||
}); | ||
|
||
expect(response.statusCode).toBe(401); | ||
expect(response.json()).toEqual({ | ||
error: 'UnauthorizedError', | ||
errors: { | ||
_: ['Unauthorized'], | ||
}, | ||
statusCode: 401, | ||
}); | ||
}); | ||
|
||
it('should return status 401, when folder name is not provided', async () => { | ||
const { accessToken } = await authService.createTokens(user.id); | ||
|
||
|
@@ -142,7 +184,7 @@ describe('POST /api/folder', () => { | |
expect(response.json()).toEqual({ | ||
error: 'ValidationError', | ||
errors: { | ||
parentId: ['Item id must be a number'], | ||
parentId: ['Parent id must be a number'], | ||
}, | ||
statusCode: 400, | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import FolderService from './folder.service'; | ||
import ItemService from '../item.service'; | ||
|
||
export default class AccessService { | ||
private itemService: ItemService; | ||
private folderService: FolderService; | ||
|
||
constructor(itemService: ItemService, folderService: FolderService) { | ||
this.itemService = itemService; | ||
this.folderService = folderService; | ||
} | ||
|
||
public async hasAccessToItem(itemId: number, userId: number): Promise<boolean> { | ||
const item = await this.itemService.getById(itemId); | ||
|
||
if (item.ownerId === userId) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters