You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
demo-graphql-oauth/src/integration/figure_upload.spec.ts

65 lines
1.8 KiB

import fetch from "node-fetch"
import { gqlUri, uploadDir } from "../server/testing"
import FormData = require("form-data")
import fs = require("fs-extra")
describe("figure upload integration should", () => {
it("upload a file", async () => {
const filename = "ok.png"
const path = uploadDir + filename
const body = bodyFrom(filename)
const reponse = await fetchWith(body)
const uploadedFile = await fs.access(path, fs.constants.F_OK)
expect(uploadedFile).toBeUndefined()
expect(reponse.errors).toBeUndefined()
expect(reponse.data).toMatchObject({ uploadFigure: true })
})
it("revoke a non-picture file", async () => {
const filename = "file.txt"
const path = uploadDir + filename
const body = bodyFrom(filename)
const response = await fetchWith(body)
await expect(fs.access(path, fs.constants.F_OK)).rejects.toThrow()
expect(response.errors).toBeDefined()
expect(response.data).toBeNull()
})
})
const fetchWith = async (body: FormData): Promise<FetchResponse> => {
const reponse = await fetch(gqlUri, {
method: "POST",
body,
})
return reponse.json()
}
const bodyFrom = (filename: string) => {
const body = new FormData()
body.append(
"operations",
JSON.stringify({
query: /* GraphQL */ `
mutation($file: Upload!) {
uploadFigure(file: $file)
}
`,
variables: {
file: null,
},
})
)
body.append("map", JSON.stringify({ 0: ["variables.file"] }))
body.append("0", "", { filename })
return body
}
interface FetchResponse {
data: any
errors?: any
}