Skip to content

Commit

Permalink
fix(sdk): console stops working when operation is unsupported (#7132)
Browse files Browse the repository at this point in the history
Co-authored-by: wingbot <[email protected]>
Co-authored-by: monada-bot[bot] <[email protected]>
  • Loading branch information
3 people committed Sep 16, 2024
1 parent 6b016b1 commit 586e9bf
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 81 deletions.
1 change: 1 addition & 0 deletions packages/@winglang/sdk/src/cloud/bucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ export class Bucket extends Resource {
BucketInflightMethods.TRY_GET,
BucketInflightMethods.TRY_GET_JSON,
BucketInflightMethods.TRY_DELETE,
BucketInflightMethods.SIGNED_URL,
BucketInflightMethods.METADATA,
BucketInflightMethods.COPY,
BucketInflightMethods.RENAME,
Expand Down
2 changes: 1 addition & 1 deletion packages/@winglang/sdk/src/target-sim/bucket.inflight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ export class Bucket implements IBucketClient, ISimulatorResourceInstance {
message: `Signed URL (key=${key})`,
activity: async () => {
throw new Error(
`signedUrl is not implemented yet for sim (key=${key})`
`signedUrl is not implemented yet for the simulator (key=${key})`
);
},
});
Expand Down
1 change: 1 addition & 0 deletions packages/@winglang/sdk/src/target-sim/bucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export class Bucket extends cloud.Bucket implements ISimulatorResource {
[cloud.BucketInflightMethods.TRY_GET]: [],
[cloud.BucketInflightMethods.TRY_GET_JSON]: [],
[cloud.BucketInflightMethods.TRY_DELETE]: [],
[cloud.BucketInflightMethods.SIGNED_URL]: [],
[cloud.BucketInflightMethods.METADATA]: [],
[cloud.BucketInflightMethods.COPY]: [],
[cloud.BucketInflightMethods.RENAME]: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ async exists(...args) { return this.backend.exists(...args); }
async tryGet(...args) { return this.backend.tryGet(...args); }
async tryGetJson(...args) { return this.backend.tryGetJson(...args); }
async tryDelete(...args) { return this.backend.tryDelete(...args); }
async signedUrl(...args) { return this.backend.signedUrl(...args); }
async metadata(...args) { return this.backend.metadata(...args); }
async copy(...args) { return this.backend.copy(...args); }
async rename(...args) { return this.backend.rename(...args); }
Expand Down
20 changes: 15 additions & 5 deletions packages/@winglang/sdk/test/target-sim/bucket.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,6 @@ test("bucket on event creates 3 topics, and sends the right event and key in the
bucket.onEvent(testInflight);

const s = await app.startSimulator();
s.onTrace({
callback: (trace) => {
console.log(trace);
},
});
const client = s.getResource("/my_bucket") as cloud.IBucketClient;
const logClient = s.getResource("/log_bucket") as cloud.IBucketClient;

Expand Down Expand Up @@ -943,6 +938,21 @@ test("bucket ignores corrupted state file", async () => {
expect(files).toEqual(["b"]);
});

test("signedUrl is not implemented for the simulator", async () => {
// GIVEN
const app = new SimApp();
new cloud.Bucket(app, "my_bucket");

const s = await app.startSimulator();
const client = s.getResource("/my_bucket") as cloud.IBucketClient;

// THEN
await expect(() => client.signedUrl("key")).rejects.toThrowError(
"signedUrl is not implemented yet"
);
await s.stop();
});

// Deceided to seperate this feature in a different release,(see https://github.com/winglang/wing/issues/4143)

// test("Given a bucket when reaching to a non existent key, signed url it should throw an error", async () => {
Expand Down
138 changes: 71 additions & 67 deletions tests/sdk_tests/bucket/signed_url.test.w
Original file line number Diff line number Diff line change
Expand Up @@ -5,94 +5,98 @@ bring expect;

let bucket = new cloud.Bucket();

test "signedUrl GET (implicit)" {
let KEY = "tempfile.txt";
let VALUE = "Hello, Wing!";
// TODO: signedUrl is not implemented for the simulator yet
// https://github.com/winglang/wing/issues/1383
if util.env("WING_TARGET") != "sim" {
test "signedUrl GET (implicit)" {
let KEY = "tempfile.txt";
let VALUE = "Hello, Wing!";

bucket.put(KEY, VALUE);
bucket.put(KEY, VALUE);

let getSignedUrl = bucket.signedUrl(KEY);
let getSignedUrl = bucket.signedUrl(KEY);

// Download file from private bucket using GET presigned URL
let output = util.shell("curl \"{getSignedUrl}\"");
// Download file from private bucket using GET presigned URL
let output = util.shell("curl \"{getSignedUrl}\"");

expect.equal(output, VALUE);
}
expect.equal(output, VALUE);
}

test "signedUrl GET (explicit)" {
let KEY = "tempfile.txt";
let VALUE = "Hello, Wing!";
test "signedUrl GET (explicit)" {
let KEY = "tempfile.txt";
let VALUE = "Hello, Wing!";

bucket.put(KEY, VALUE);
bucket.put(KEY, VALUE);

let getSignedUrl = bucket.signedUrl(KEY, { action: cloud.BucketSignedUrlAction.DOWNLOAD });
let getSignedUrl = bucket.signedUrl(KEY, { action: cloud.BucketSignedUrlAction.DOWNLOAD });

// Download file from private bucket using GET presigned URL
let output = util.shell("curl \"{getSignedUrl}\"");
// Download file from private bucket using GET presigned URL
let output = util.shell("curl \"{getSignedUrl}\"");

expect.equal(output, VALUE);
}
expect.equal(output, VALUE);
}

test "signedUrl GET with non-existent key" {
let assertThrows = (expected: str, block: (): void) => {
let var error = false;
try {
block();
} catch actual {
expect.equal(actual, expected);
error = true;
}
expect.equal(error, true);
};
let UNEXISTING_KEY = "no-such-file.txt";
let OBJECT_DOES_NOT_EXIST_ERROR = "Cannot provide signed url for a non-existent key (key={UNEXISTING_KEY})";

assertThrows(OBJECT_DOES_NOT_EXIST_ERROR, () => {
bucket.signedUrl(UNEXISTING_KEY);
});
}
test "signedUrl GET with non-existent key" {
let assertThrows = (expected: str, block: (): void) => {
let var error = false;
try {
block();
} catch actual {
expect.equal(actual, expected);
error = true;
}
expect.equal(error, true);
};
let UNEXISTING_KEY = "no-such-file.txt";
let OBJECT_DOES_NOT_EXIST_ERROR = "Cannot provide signed url for a non-existent key (key={UNEXISTING_KEY})";

test "signedUrl PUT" {
let KEY = "tempfile.txt";
let VALUE = "Hello, Wing!";
assertThrows(OBJECT_DOES_NOT_EXIST_ERROR, () => {
bucket.signedUrl(UNEXISTING_KEY);
});
}

let tempDir = fs.mkdtemp();
let tempFile = fs.join(tempDir, KEY);
fs.writeFile(tempFile, VALUE);
test "signedUrl PUT" {
let KEY = "tempfile.txt";
let VALUE = "Hello, Wing!";

let putSignedUrl = bucket.signedUrl(KEY, { action: cloud.BucketSignedUrlAction.UPLOAD });
let tempDir = fs.mkdtemp();
let tempFile = fs.join(tempDir, KEY);
fs.writeFile(tempFile, VALUE);

// Upload file to private bucket using PUT presigned URL
util.shell("curl -X PUT -T \"{tempFile}\" \"{putSignedUrl}\"");
let putSignedUrl = bucket.signedUrl(KEY, { action: cloud.BucketSignedUrlAction.UPLOAD });

expect.equal(bucket.get(KEY), VALUE);
}
// Upload file to private bucket using PUT presigned URL
util.shell("curl -X PUT -T \"{tempFile}\" \"{putSignedUrl}\"");

expect.equal(bucket.get(KEY), VALUE);
}

test "signedUrl duration option is respected" {
let isExpiredTokenError = (output: str) => {
let target = util.env("WING_TARGET");
let var result = false;

test "signedUrl duration option is respected" {
let isExpiredTokenError = (output: str) => {
let target = util.env("WING_TARGET");
let var result = false;
if target == "tf-aws" {
result = output.contains("<Code>AccessDenied</Code><Message>Request has expired</Message>");
} else if target == "tf-gcp" {
result = output.contains("<Code>ExpiredToken</Code><Message>Invalid argument.</Message>");
}

if target == "tf-aws" {
result = output.contains("<Code>AccessDenied</Code><Message>Request has expired</Message>");
} else if target == "tf-gcp" {
result = output.contains("<Code>ExpiredToken</Code><Message>Invalid argument.</Message>");
}

return result;
};
return result;
};

let KEY = "tempfile.txt";
let VALUE = "Hello, Wing!";
let KEY = "tempfile.txt";
let VALUE = "Hello, Wing!";

bucket.put(KEY, VALUE);
bucket.put(KEY, VALUE);

let getSignedUrl = bucket.signedUrl(KEY, { duration: 1s });
let getSignedUrl = bucket.signedUrl(KEY, { duration: 1s });

util.sleep(2s);
util.sleep(2s);

// Download file from private bucket using expired GET presigned URL
let output = util.shell("curl \"{getSignedUrl}\"");
// Download file from private bucket using expired GET presigned URL
let output = util.shell("curl \"{getSignedUrl}\"");

expect.equal(isExpiredTokenError(output), true);
expect.equal(isExpiredTokenError(output), true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

## stdout.log
```log
Error: Resource root/Default/Bucket does not support inflight operation signedUrl.
It might not be implemented yet.
pass ─ cors.test.wsim (no tests)
Tests 1 unsupported (1)
Tests 1 passed (1)
Snapshots 1 skipped
Test Files 1 unsupported (1)
Test Files 1 passed (1)
Duration <DURATION>
```

Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

## stdout.log
```log
Error: Resource root/Default/Bucket does not support inflight operation signedUrl.
It might not be implemented yet.
pass ─ signed_url.test.wsim (no tests)
Tests 1 unsupported (1)
Tests 1 passed (1)
Snapshots 1 skipped
Test Files 1 unsupported (1)
Test Files 1 passed (1)
Duration <DURATION>
```

0 comments on commit 586e9bf

Please sign in to comment.