Skip to content

Commit

Permalink
Merge pull request #143 from Akord-com/fix-file-uploads
Browse files Browse the repository at this point in the history
fix vault context for file uploads
  • Loading branch information
wkolod authored May 25, 2023
2 parents be5d268 + 667d8ca commit d807046
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 13 deletions.
6 changes: 4 additions & 2 deletions src/__tests__/common.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
require("dotenv").config();
import { Akord, Auth } from "../index";
import faker from '@faker-js/faker';

export async function initInstance(email: string, password: string): Promise<Akord> {
Auth.configure({ env: process.env.ENV as any });
const { wallet } = await Auth.signIn(email, password);
return new Akord(wallet, { debug: true });
return new Akord(wallet, { debug: true, env: process.env.ENV as any });
}

export const vaultCreate = async (akord: Akord) => {
const name = faker.random.words();
const termsOfAccess = faker.lorem.sentences();
const { vaultId, membershipId } = await akord.vault.create(name, { termsOfAccess });
const { vaultId, membershipId } = await akord.vault.create(name, { termsOfAccess, cacheOnly: true });

const membership = await akord.membership.get(membershipId);
expect(membership.status).toEqual("ACCEPTED");
Expand Down
3 changes: 2 additions & 1 deletion src/__tests__/folder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Akord } from "../index";
import faker from '@faker-js/faker';
import { initInstance, folderCreate, vaultCreate } from './common';
import { email, password } from './data/test-credentials';
import { BadRequest } from "../errors/bad-request";

let akord: Akord;

Expand Down Expand Up @@ -43,7 +44,7 @@ describe("Testing folder functions", () => {
const name = faker.random.words();
await expect(async () =>
await akord.folder.create(vaultId, name, { parentId: rootFolderId })
).rejects.toThrow(Error);
).rejects.toThrow(BadRequest);
});

it("should restore root folder", async () => {
Expand Down
3 changes: 2 additions & 1 deletion src/__tests__/membership.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Akord } from "../index";
import { email, email2, password, password2 } from './data/test-credentials';
import { initInstance, vaultCreate } from './common';
import { Membership } from "../types/membership";
import { BadRequest } from "../errors/bad-request";

let ownerAkordInstance: Akord;
let inviteeAkordInstance: Akord;
Expand Down Expand Up @@ -84,7 +85,7 @@ describe("Testing membership functions", () => {
ownerAkordInstance = await initInstance(email, password);
await expect(async () =>
ownerAkordInstance.membership.invite(vaultId, email2, "VIEWER")
).rejects.toThrow(Error);
).rejects.toThrow(BadRequest);
});

it("should change access", async () => {
Expand Down
5 changes: 3 additions & 2 deletions src/__tests__/queries.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ jest.setTimeout(3000000);

describe("Testing querying directly from permaweb", () => {
beforeAll(async () => {
Auth.configure({ env: process.env.ENV as any });
const { wallet } = await Auth.signIn(email, password);
privateClient = new Akord(wallet);
publicClient = new Akord();
privateClient = new Akord(wallet, { debug: true, env: process.env.ENV as any });
publicClient = new Akord(undefined, { debug: true, env: process.env.ENV as any });
});

it("Query all vaults from Akord API", async () => {
Expand Down
3 changes: 2 additions & 1 deletion src/__tests__/vault.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Akord } from "../index";
import faker from '@faker-js/faker';
import { initInstance, vaultCreate } from './common';
import { email, password } from './data/test-credentials';
import { BadRequest } from "../errors/bad-request";

let akord: Akord;

Expand Down Expand Up @@ -78,7 +79,7 @@ describe("Testing vault functions", () => {
const name = faker.random.words();
await expect(async () =>
await akord.vault.rename(vaultId, name)
).rejects.toThrow(Error);
).rejects.toThrow(BadRequest);
});

it("should restore the vault", async () => {
Expand Down
2 changes: 1 addition & 1 deletion src/core/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ class NodeService<T = NodeLike> extends Service {
}

protected async setVaultContextFromNodeId(nodeId: string, type: NodeType, vaultId?: string) {
const object = await this.api.getNode<NodeLike>(nodeId, type, this.vaultId);
const object = await this.api.getNode<NodeLike>(nodeId, type, vaultId);
this.setVaultId(object.vaultId);
this.setIsPublic(object.__public__);
await this.setMembershipKeys(object);
Expand Down
14 changes: 9 additions & 5 deletions src/core/stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@ class StackService extends NodeService<Stack> {
...options
}
await this.setVaultContext(vaultId);
this.setVaultContextForFile();
this.setActionRef(actionRefs.STACK_CREATE);
this.setFunction(functions.NODE_CREATE);
this.setTags(createOptions.tags);

createOptions.cacheOnly = this.vault.cacheOnly;

const state = {
name: await this.processWriteString(name ? name : file.name),
versions: [await this.uploadNewFileVersion(file, createOptions)],
Expand All @@ -44,6 +47,7 @@ class StackService extends NodeService<Stack> {
*/
public async import(vaultId: string, fileTxId: string, options: NodeCreateOptions = this.defaultCreateOptions): Promise<StackCreateResult> {
await this.setVaultContext(vaultId);
this.setVaultContextForFile();
this.setActionRef(actionRefs.STACK_CREATE);
this.setFunction(functions.NODE_CREATE);

Expand Down Expand Up @@ -72,8 +76,11 @@ class StackService extends NodeService<Stack> {
*/
public async uploadRevision(stackId: string, file: FileLike, options: FileUploadOptions = {}): Promise<StackUpdateResult> {
await this.setVaultContextFromNodeId(stackId, this.objectType);
this.setVaultContextForFile();
this.setActionRef(actionRefs.STACK_UPLOAD_REVISION);

options.cacheOnly = this.object.__cacheOnly__;

const state = {
versions: [await this.uploadNewFileVersion(file, options)]
};
Expand All @@ -91,6 +98,7 @@ class StackService extends NodeService<Stack> {
const stack = new Stack(await this.api.getNode<Stack>(stackId, objectType.STACK, this.vaultId), null);
const version = stack.getVersion(index);
await this.setVaultContext(stack.vaultId);
this.setVaultContextForFile();
const { fileData, metadata } = await this.api.downloadFile(version.getUri(StorageType.S3), { public: this.isPublic });
const data = await this.processReadRaw(fileData, metadata);
const name = await this.processReadString(version.name);
Expand All @@ -110,9 +118,6 @@ class StackService extends NodeService<Stack> {
}

private async uploadNewFileVersion(file: FileLike, options: FileUploadOptions): Promise<FileVersion> {
if (this.object.__cacheOnly__) {
options.cacheOnly = true
}
const {
resourceTx,
resourceUrl,
Expand All @@ -133,8 +138,7 @@ class StackService extends NodeService<Stack> {
return version;
}

protected async setVaultContext(vaultId: string): Promise<void> {
await super.setVaultContext(vaultId);
protected setVaultContextForFile(): void {
this.fileService.setKeys(this.keys);
this.fileService.setRawDataEncryptionPublicKey(this.dataEncrypter.publicKey);
this.fileService.setVaultId(this.vaultId);
Expand Down

0 comments on commit d807046

Please sign in to comment.