|
| 1 | +import getI18nTranslator from "./i18n"; |
| 2 | +import run, { validateDataSourceConfig } from "./run"; |
| 3 | + |
| 4 | +const dataSourceConfig = { |
| 5 | + accessKey: "", |
| 6 | + secretKey: "", |
| 7 | + endpointUrl: "", |
| 8 | + region: "us-west-2", |
| 9 | +}; |
| 10 | + |
| 11 | +const bucket = "openblocks-demo"; |
| 12 | +const i18n = getI18nTranslator(["en"]); |
| 13 | + |
| 14 | +describe.skip("s3 plugin", () => { |
| 15 | + test("validate data source config", async () => { |
| 16 | + const a = await validateDataSourceConfig(dataSourceConfig); |
| 17 | + expect(a.success).toBe(true); |
| 18 | + |
| 19 | + const b = await validateDataSourceConfig({ |
| 20 | + ...dataSourceConfig, |
| 21 | + accessKey: "error ak", |
| 22 | + }); |
| 23 | + console.info(b.message); |
| 24 | + expect(b.success).toBe(false); |
| 25 | + }); |
| 26 | + |
| 27 | + test("read not existed file", async () => { |
| 28 | + await expect( |
| 29 | + run( |
| 30 | + { |
| 31 | + actionName: "readFile", |
| 32 | + bucket, |
| 33 | + fileName: "not-found.txt", |
| 34 | + encoding: "utf8", |
| 35 | + }, |
| 36 | + dataSourceConfig, |
| 37 | + i18n |
| 38 | + ) |
| 39 | + ).rejects.toThrow(); |
| 40 | + }); |
| 41 | + |
| 42 | + test("list buckets", async () => { |
| 43 | + const buckets = await run( |
| 44 | + { |
| 45 | + actionName: "listBuckets", |
| 46 | + }, |
| 47 | + dataSourceConfig, |
| 48 | + i18n |
| 49 | + ); |
| 50 | + expect(buckets?.length).toBeGreaterThan(0); |
| 51 | + }); |
| 52 | + |
| 53 | + test("crud file", async () => { |
| 54 | + const txtFileName = `ut-${Date.now()}.txt`; |
| 55 | + const binFileName = `ut-${Date.now()}.txt`; |
| 56 | + const fileData = `This is file ${txtFileName}`; |
| 57 | + const binRawFileData = `This is file ${binFileName}`; |
| 58 | + const binFileData = Buffer.from(binRawFileData).toString("base64"); |
| 59 | + |
| 60 | + // upload txt |
| 61 | + const uploadRes = await run( |
| 62 | + { |
| 63 | + actionName: "uploadData", |
| 64 | + fileName: txtFileName, |
| 65 | + encoding: "utf8", |
| 66 | + contentType: "text/plain", |
| 67 | + data: fileData, |
| 68 | + bucket, |
| 69 | + returnSignedUrl: true, |
| 70 | + }, |
| 71 | + dataSourceConfig, |
| 72 | + i18n |
| 73 | + ); |
| 74 | + expect((uploadRes as any).signedUrl).not.toBe(""); |
| 75 | + |
| 76 | + // upload bin |
| 77 | + const uploadBinRes = await run( |
| 78 | + { |
| 79 | + actionName: "uploadData", |
| 80 | + fileName: binFileName, |
| 81 | + encoding: "base64", |
| 82 | + contentType: "", |
| 83 | + data: binFileData, |
| 84 | + bucket, |
| 85 | + returnSignedUrl: true, |
| 86 | + }, |
| 87 | + dataSourceConfig, |
| 88 | + i18n |
| 89 | + ); |
| 90 | + expect((uploadBinRes as any).signedUrl).not.toBe(""); |
| 91 | + |
| 92 | + // list |
| 93 | + const list = await run( |
| 94 | + { |
| 95 | + actionName: "listObjects", |
| 96 | + bucket, |
| 97 | + prefix: txtFileName, |
| 98 | + delimiter: "", |
| 99 | + limit: 10, |
| 100 | + returnSignedUrl: false, |
| 101 | + }, |
| 102 | + dataSourceConfig, |
| 103 | + i18n |
| 104 | + ); |
| 105 | + expect((list as any).length).toBeGreaterThan(0); |
| 106 | + expect((list as any).find((i: any) => i.name === txtFileName)).not.toBeUndefined(); |
| 107 | + expect((list as any).find((i: any) => i.name === binFileName)).not.toBeUndefined(); |
| 108 | + |
| 109 | + // read txt |
| 110 | + const readRes = await run( |
| 111 | + { |
| 112 | + actionName: "readFile", |
| 113 | + bucket, |
| 114 | + fileName: txtFileName, |
| 115 | + encoding: "utf8", |
| 116 | + }, |
| 117 | + dataSourceConfig, |
| 118 | + i18n |
| 119 | + ); |
| 120 | + expect((readRes as any).data).toEqual(fileData); |
| 121 | + |
| 122 | + // read bin |
| 123 | + const readBinRes = await run( |
| 124 | + { |
| 125 | + actionName: "readFile", |
| 126 | + bucket, |
| 127 | + fileName: binFileName, |
| 128 | + encoding: "base64", |
| 129 | + }, |
| 130 | + dataSourceConfig, |
| 131 | + i18n |
| 132 | + ); |
| 133 | + expect((readBinRes as any).data).toEqual(binFileData); |
| 134 | + |
| 135 | + // delete |
| 136 | + const delRes = await run( |
| 137 | + { |
| 138 | + actionName: "deleteFile", |
| 139 | + bucket, |
| 140 | + fileName: txtFileName, |
| 141 | + }, |
| 142 | + dataSourceConfig, |
| 143 | + i18n |
| 144 | + ); |
| 145 | + expect((delRes as any).success).toBe(true); |
| 146 | + |
| 147 | + const delBinRes = await run( |
| 148 | + { |
| 149 | + actionName: "deleteFile", |
| 150 | + bucket, |
| 151 | + fileName: binFileName, |
| 152 | + }, |
| 153 | + dataSourceConfig, |
| 154 | + i18n |
| 155 | + ); |
| 156 | + expect((delBinRes as any).success).toBe(true); |
| 157 | + |
| 158 | + // delete not empty file |
| 159 | + expect( |
| 160 | + run( |
| 161 | + { |
| 162 | + actionName: "deleteFile", |
| 163 | + bucket, |
| 164 | + fileName: "", |
| 165 | + }, |
| 166 | + dataSourceConfig, |
| 167 | + i18n |
| 168 | + ) |
| 169 | + ).rejects.toThrow(); |
| 170 | + }); |
| 171 | +}); |
0 commit comments