-
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.
#27 - Added & refactored sharing endpoints
1 parent
3a6b0c2
commit db0de0e
Showing
13 changed files
with
785 additions
and
27 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
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,281 @@ | ||
import { User } from '@prisma/client'; | ||
import UserService from '../../auth/user.service'; | ||
import FolderService from '../folder/folder.service'; | ||
import AuthService from '../../auth/auth.service'; | ||
import BlobService from '../blob/blob.service'; | ||
import DocsService from '../docs/docs.service'; | ||
import ShortcutService from '../shortcut/shortcut.service'; | ||
import SharingService from '../sharing/sharing.service'; | ||
import ItemService from '../item.service'; | ||
|
||
describe('GET /api/item/shared', () => { | ||
let userService: UserService; | ||
let folderService: FolderService; | ||
let authService: AuthService; | ||
let blobService: BlobService; | ||
let docsService: DocsService; | ||
let shortcutService: ShortcutService; | ||
let sharingService: SharingService; | ||
|
||
let user: User; | ||
let otherUser: User; | ||
|
||
beforeAll(async () => { | ||
userService = new UserService(); | ||
folderService = new FolderService(); | ||
authService = new AuthService(); | ||
blobService = new BlobService(); | ||
docsService = new DocsService(); | ||
shortcutService = new ShortcutService(); | ||
sharingService = new SharingService(new ItemService()); | ||
|
||
user = await userService.createUser({ | ||
name: 'Joe Biden the 1st', | ||
email: 'joe@biden.com', | ||
password: '1234', | ||
}); | ||
otherUser = await userService.createUser({ | ||
name: 'Joe Biden the 2nd', | ||
email: 'joe2@biden.com', | ||
password: '4321', | ||
}); | ||
}); | ||
|
||
it('Should return status 200 and all items from parentId', async () => { | ||
const { accessToken } = await authService.createTokens(user.id); | ||
|
||
const folder1 = await folderService.createFolder({ | ||
name: 'Folder1', | ||
color: '#123456', | ||
ownerId: otherUser.id, | ||
parentId: null, | ||
}); | ||
await sharingService.createSharing( | ||
{ | ||
itemId: folder1.id, | ||
userId: user.id, | ||
}, | ||
otherUser.id, | ||
); | ||
|
||
const blob = await blobService.createBlob({ | ||
mimeType: 'text/plain', | ||
name: 'test1.txt', | ||
ownerId: otherUser.id, | ||
parentId: folder1.id, | ||
blobUrl: 'https://example.com/test1.txt', | ||
}); | ||
await sharingService.createSharing( | ||
{ | ||
itemId: blob.id, | ||
userId: user.id, | ||
}, | ||
otherUser.id, | ||
); | ||
|
||
const folder2 = await folderService.createFolder({ | ||
name: 'Folder2', | ||
color: '#987654', | ||
ownerId: otherUser.id, | ||
parentId: folder1.id, | ||
}); | ||
await sharingService.createSharing( | ||
{ | ||
itemId: folder2.id, | ||
userId: user.id, | ||
}, | ||
otherUser.id, | ||
); | ||
|
||
const docs = await docsService.createDocs({ | ||
name: 'Docs1', | ||
text: 'Docs1 text', | ||
ownerId: otherUser.id, | ||
parentId: folder1.id, | ||
}); | ||
await sharingService.createSharing( | ||
{ | ||
itemId: docs.id, | ||
userId: user.id, | ||
}, | ||
otherUser.id, | ||
); | ||
|
||
const shortcut = await shortcutService.createShortcut({ | ||
name: 'Shortcut', | ||
ownerId: otherUser.id, | ||
linkedItemId: blob.id, | ||
parentId: folder1.id, | ||
}); | ||
await sharingService.createSharing( | ||
{ | ||
itemId: shortcut.id, | ||
userId: user.id, | ||
}, | ||
otherUser.id, | ||
); | ||
|
||
const response = await global.fastify.inject({ | ||
method: 'GET', | ||
url: '/api/item/shared', | ||
headers: { | ||
authorization: 'Bearer ' + accessToken, | ||
}, | ||
}); | ||
|
||
console.log(response.json()); | ||
|
||
expect(response.statusCode).toBe(200); | ||
expect(response.json()).toEqual([ | ||
{ | ||
id: expect.any(Number), | ||
name: 'Folder1', | ||
color: '#123456', | ||
parentId: null, | ||
ownerId: otherUser.id, | ||
isStarred: false, | ||
mimeType: 'application/vnd.cloudstore.folder', | ||
createdAt: expect.any(String), | ||
deletedAt: null, | ||
updatedAt: expect.any(String), | ||
}, | ||
{ | ||
id: expect.any(Number), | ||
name: 'Folder2', | ||
color: '#987654', | ||
parentId: folder1.id, | ||
ownerId: otherUser.id, | ||
isStarred: false, | ||
mimeType: 'application/vnd.cloudstore.folder', | ||
createdAt: expect.any(String), | ||
deletedAt: null, | ||
updatedAt: expect.any(String), | ||
}, | ||
{ | ||
id: expect.any(Number), | ||
name: 'Shortcut', | ||
parentId: folder1.id, | ||
ownerId: otherUser.id, | ||
isStarred: false, | ||
mimeType: 'application/vnd.cloudstore.shortcut', | ||
createdAt: expect.any(String), | ||
deletedAt: null, | ||
updatedAt: expect.any(String), | ||
}, | ||
{ | ||
id: expect.any(Number), | ||
name: 'Docs1', | ||
text: 'Docs1 text', | ||
parentId: folder1.id, | ||
ownerId: otherUser.id, | ||
isStarred: false, | ||
mimeType: 'application/vnd.cloudstore.docs', | ||
createdAt: expect.any(String), | ||
deletedAt: null, | ||
updatedAt: expect.any(String), | ||
}, | ||
{ | ||
id: expect.any(Number), | ||
name: 'test1.txt', | ||
blobUrl: 'https://example.com/test1.txt', | ||
parentId: folder1.id, | ||
ownerId: otherUser.id, | ||
isStarred: false, | ||
mimeType: 'text/plain', | ||
createdAt: expect.any(String), | ||
deletedAt: null, | ||
updatedAt: expect.any(String), | ||
}, | ||
]); | ||
}); | ||
|
||
it('Should return status 401, when unauthorized', async () => { | ||
const folder1 = await folderService.createFolder({ | ||
name: 'Folder1', | ||
color: '#123456', | ||
ownerId: otherUser.id, | ||
parentId: null, | ||
}); | ||
await sharingService.createSharing( | ||
{ | ||
itemId: folder1.id, | ||
userId: user.id, | ||
}, | ||
otherUser.id, | ||
); | ||
|
||
const blob = await blobService.createBlob({ | ||
mimeType: 'text/plain', | ||
name: 'test1.txt', | ||
ownerId: otherUser.id, | ||
parentId: folder1.id, | ||
blobUrl: 'https://example.com/test1.txt', | ||
}); | ||
await sharingService.createSharing( | ||
{ | ||
itemId: blob.id, | ||
userId: user.id, | ||
}, | ||
otherUser.id, | ||
); | ||
|
||
const folder2 = await folderService.createFolder({ | ||
name: 'Folder2', | ||
color: '#987654', | ||
ownerId: otherUser.id, | ||
parentId: folder1.id, | ||
}); | ||
await sharingService.createSharing( | ||
{ | ||
itemId: folder2.id, | ||
userId: user.id, | ||
}, | ||
otherUser.id, | ||
); | ||
|
||
const docs = await docsService.createDocs({ | ||
name: 'Docs1', | ||
text: 'Docs1 text', | ||
ownerId: otherUser.id, | ||
parentId: folder1.id, | ||
}); | ||
await sharingService.createSharing( | ||
{ | ||
itemId: docs.id, | ||
userId: user.id, | ||
}, | ||
otherUser.id, | ||
); | ||
|
||
const shortcut = await shortcutService.createShortcut({ | ||
name: 'Shortcut', | ||
ownerId: otherUser.id, | ||
linkedItemId: blob.id, | ||
parentId: folder1.id, | ||
}); | ||
await sharingService.createSharing( | ||
{ | ||
itemId: shortcut.id, | ||
userId: user.id, | ||
}, | ||
otherUser.id, | ||
); | ||
|
||
const response = await global.fastify.inject({ | ||
method: 'GET', | ||
url: '/api/item/shared', | ||
headers: { | ||
authorization: 'invalid_token!!', | ||
}, | ||
}); | ||
|
||
expect(response.statusCode).toBe(401); | ||
expect(response.json()).toEqual({ | ||
error: 'UnauthorizedError', | ||
errors: { | ||
_: ['Unauthorized'], | ||
}, | ||
statusCode: 401, | ||
}); | ||
}); | ||
}); |
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,177 @@ | ||
import { User } from '@prisma/client'; | ||
import UserService from '../../auth/user.service'; | ||
import FolderService from '../folder/folder.service'; | ||
import AuthService from '../../auth/auth.service'; | ||
import SharingService from '../sharing/sharing.service'; | ||
import ItemService from '../item.service'; | ||
|
||
describe('GET /api/item/:id/sharings', () => { | ||
let userService: UserService; | ||
let folderService: FolderService; | ||
let authService: AuthService; | ||
let sharingService: SharingService; | ||
|
||
let user: User; | ||
let otherUser: User; | ||
|
||
beforeAll(async () => { | ||
userService = new UserService(); | ||
folderService = new FolderService(); | ||
authService = new AuthService(); | ||
sharingService = new SharingService(new ItemService()); | ||
|
||
user = await userService.createUser({ | ||
name: 'Joe Biden the 1st', | ||
email: 'joe@biden.com', | ||
password: '1234', | ||
}); | ||
otherUser = await userService.createUser({ | ||
name: 'Joe Biden the 2nd', | ||
email: 'joe2@biden.com', | ||
password: '4321', | ||
}); | ||
}); | ||
|
||
it("Should return status 200, item, it's sharings, their users and the owner by itemId", async () => { | ||
const { accessToken } = await authService.createTokens(user.id); | ||
|
||
const folder = await folderService.createFolder({ | ||
name: 'Folder1', | ||
color: '#123456', | ||
ownerId: user.id, | ||
parentId: null, | ||
}); | ||
const sharing = await sharingService.createSharing( | ||
{ | ||
itemId: folder.id, | ||
userId: otherUser.id, | ||
}, | ||
user.id, | ||
); | ||
|
||
const response = await global.fastify.inject({ | ||
method: 'GET', | ||
url: `/api/item/${folder.id}/sharings`, | ||
headers: { | ||
authorization: 'Bearer ' + accessToken, | ||
}, | ||
}); | ||
|
||
expect(response.statusCode).toBe(200); | ||
expect(response.json()).toEqual({ | ||
id: expect.any(Number), | ||
name: 'Folder1', | ||
parentId: null, | ||
ownerId: user.id.toString(), | ||
owner: { | ||
id: user.id, | ||
name: user.name, | ||
email: user.email, | ||
createdAt: expect.any(String), | ||
updatedAt: expect.any(String), | ||
}, | ||
ItemSharing: [ | ||
{ | ||
id: sharing.id, | ||
userId: sharing.userId, | ||
user: { | ||
id: otherUser.id, | ||
name: otherUser.name, | ||
email: otherUser.email, | ||
createdAt: expect.any(String), | ||
updatedAt: expect.any(String), | ||
}, | ||
itemId: sharing.itemId, | ||
createdAt: expect.any(String), | ||
updatedAt: expect.any(String), | ||
}, | ||
], | ||
mimeType: 'application/vnd.cloudstore.folder', | ||
createdAt: expect.any(String), | ||
updatedAt: expect.any(String), | ||
deletedAt: null, | ||
}); | ||
}); | ||
|
||
it("Should return status 200, item, it's sharings, their users and the owner by itemId", async () => { | ||
const { accessToken } = await authService.createTokens(user.id); | ||
|
||
const response = await global.fastify.inject({ | ||
method: 'GET', | ||
url: `/api/item/1234/sharings`, | ||
headers: { | ||
authorization: 'Bearer ' + accessToken, | ||
}, | ||
}); | ||
|
||
expect(response.statusCode).toBe(400); | ||
expect(response.json()).toEqual({ | ||
error: 'BadRequestError', | ||
errors: { | ||
_: ['Item not found'], | ||
}, | ||
statusCode: 400, | ||
}); | ||
}); | ||
|
||
it("Should return status 401, when you don't have access to the item", async () => { | ||
const { accessToken } = await authService.createTokens(otherUser.id); | ||
|
||
const folder = await folderService.createFolder({ | ||
name: 'Folder1', | ||
color: '#123456', | ||
ownerId: user.id, | ||
parentId: null, | ||
}); | ||
|
||
const response = await global.fastify.inject({ | ||
method: 'GET', | ||
url: `/api/item/${folder.id}/sharings`, | ||
headers: { | ||
authorization: 'Bearer ' + accessToken, | ||
}, | ||
}); | ||
|
||
expect(response.statusCode).toBe(401); | ||
expect(response.json()).toEqual({ | ||
error: 'UnauthorizedError', | ||
errors: { | ||
_: ['Unauthorized'], | ||
}, | ||
statusCode: 401, | ||
}); | ||
}); | ||
|
||
it('Should return status 401, when unauthorized', async () => { | ||
const folder = await folderService.createFolder({ | ||
name: 'Folder1', | ||
color: '#123456', | ||
ownerId: user.id, | ||
parentId: null, | ||
}); | ||
await sharingService.createSharing( | ||
{ | ||
itemId: folder.id, | ||
userId: otherUser.id, | ||
}, | ||
user.id, | ||
); | ||
|
||
const response = await global.fastify.inject({ | ||
method: 'GET', | ||
url: `/api/item/${folder.id}/sharings`, | ||
headers: { | ||
authorization: 'invalid_token!!', | ||
}, | ||
}); | ||
|
||
expect(response.statusCode).toBe(401); | ||
expect(response.json()).toEqual({ | ||
error: 'UnauthorizedError', | ||
errors: { | ||
_: ['Unauthorized'], | ||
}, | ||
statusCode: 401, | ||
}); | ||
}); | ||
}); |
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
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