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

feat: Added & refactored sharing endpoints #29

Merged
merged 1 commit into from
Oct 11, 2023
Merged
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
5 changes: 5 additions & 0 deletions src/locales/da.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@
"required": "userId er påkrævet",
"type": "userId skal være et tal"
},
"email": {
"required": "Email er påkrævet",
"type": "Email skal være en tekst",
"format": "Email skal være af korrekt format"
},
"notFound": "Delning ikke fundet",
"alreadyExists": "Delning findes allerede"
},
Expand Down
5 changes: 5 additions & 0 deletions src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@
"required": "userId is required",
"type": "userId must be a number"
},
"email": {
"required": "Email is required",
"type": "Email must be a number",
"format": "Email must be of correct format"
},
"notFound": "Sharing not found",
"alreadyExists": "Sharing already exists"
},
Expand Down
6 changes: 6 additions & 0 deletions src/modules/item/__test__/item.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,4 +419,10 @@ describe('ItemService', () => {
await expect(itemService.getById(1234)).rejects.toThrow();
});
});

describe('getItemByIdWithSharingsAndOwner()', () => {
it("should throw error, when item doesn't exist", async () => {
await expect(itemService.getItemByIdWithSharingsAndOwner(1234)).rejects.toThrow();
});
});
});
281 changes: 281 additions & 0 deletions src/modules/item/__test__/item.shared.test.ts
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: '[email protected]',
password: '1234',
});
otherUser = await userService.createUser({
name: 'Joe Biden the 2nd',
email: '[email protected]',
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,
});
});
});
Loading
Loading