budibase/packages/server/src/integrations/tests/googlesheets.spec.ts

192 lines
5.2 KiB
TypeScript
Raw Normal View History

import { setEnv as setCoreEnv } from "@budibase/backend-core"
import type { GoogleSpreadsheetWorksheet } from "google-spreadsheet"
import nock from "nock"
import { structures } from "@budibase/backend-core/tests"
import TestConfiguration from "../../tests/utilities/TestConfiguration"
2024-09-04 15:21:25 +02:00
import { GoogleSheetsConfig, GoogleSheetsIntegration } from "../googlesheets"
import {
Datasource,
FieldType,
SourceName,
Table,
TableSchema,
TableSourceType,
} from "@budibase/types"
import { generateDatasourceID } from "../../db/utils"
describe("Google Sheets Integration", () => {
2024-09-04 15:21:25 +02:00
const config = new TestConfiguration()
let integration: GoogleSheetsIntegration
let cleanupEnv: () => void
2024-09-04 15:21:25 +02:00
let table: Table
let datasource: Datasource
2024-09-04 15:21:25 +02:00
const datasourceConfig: GoogleSheetsConfig = {
spreadsheetId: "randomId",
auth: {
appId: "appId",
accessToken: "accessToken",
refreshToken: "refreshToken",
},
}
beforeAll(async () => {
cleanupEnv = setCoreEnv({
GOOGLE_CLIENT_ID: "test",
GOOGLE_CLIENT_SECRET: "test",
})
2024-09-04 15:21:25 +02:00
datasource = await config.api.datasource.create({
name: "Test Datasource",
type: "datasource",
source: SourceName.GOOGLE_SHEETS,
config: datasourceConfig,
})
2023-03-06 11:33:49 +01:00
})
afterAll(async () => {
cleanupEnv()
config.end()
})
beforeEach(async () => {
await config.init()
2023-05-23 10:17:42 +02:00
2024-09-04 15:21:25 +02:00
integration = new GoogleSheetsIntegration(datasourceConfig)
table = await config.api.table.save({
name: "Test Table",
type: "table",
sourceId: generateDatasourceID(),
sourceType: TableSourceType.EXTERNAL,
schema: {
name: {
name: "name",
type: FieldType.STRING,
constraints: {
type: "string",
},
},
description: {
name: "description",
type: FieldType.STRING,
constraints: {
type: "string",
},
},
},
})
nock.cleanAll()
nock("https://www.googleapis.com/").post("/oauth2/v4/token").reply(200, {
grant_type: "client_credentials",
client_id: "your-client-id",
client_secret: "your-client-secret",
})
})
function createBasicTable(name: string, columns: string[]): Table {
return {
type: "table",
name,
sourceId: generateDatasourceID(),
sourceType: TableSourceType.EXTERNAL,
schema: {
...columns.reduce((p, c) => {
p[c] = {
name: c,
type: FieldType.STRING,
constraints: {
type: "string",
},
}
return p
}, {} as TableSchema),
},
}
}
function createSheet({
headerValues,
}: {
headerValues: string[]
}): GoogleSpreadsheetWorksheet {
return {
// to ignore the unmapped fields
...({} as any),
loadHeaderRow: jest.fn(),
headerValues,
setHeaderRow: jest.fn(),
}
}
describe("update table", () => {
2023-05-23 10:14:06 +02:00
it("adding a new field will be adding a new header row", async () => {
await config.doInContext(structures.uuid(), async () => {
const tableColumns = ["name", "description", "new field"]
const table = createBasicTable(structures.uuid(), tableColumns)
const sheet = createSheet({ headerValues: ["name", "description"] })
sheetsByTitle[table.name] = sheet
await integration.updateTable(table)
2024-03-19 10:46:10 +01:00
expect(sheet.loadHeaderRow).toHaveBeenCalledTimes(1)
expect(sheet.setHeaderRow).toHaveBeenCalledTimes(1)
expect(sheet.setHeaderRow).toHaveBeenCalledWith(tableColumns)
})
})
2023-05-23 10:14:06 +02:00
it("removing an existing field will remove the header from the google sheet", async () => {
2023-03-21 18:27:31 +01:00
const sheet = await config.doInContext(structures.uuid(), async () => {
const tableColumns = ["name"]
const table = createBasicTable(structures.uuid(), tableColumns)
const sheet = createSheet({
headerValues: ["name", "description", "location"],
})
sheetsByTitle[table.name] = sheet
await integration.updateTable(table)
2023-03-21 18:27:31 +01:00
return sheet
})
2024-03-19 10:46:10 +01:00
expect(sheet.loadHeaderRow).toHaveBeenCalledTimes(1)
expect(sheet.setHeaderRow).toHaveBeenCalledTimes(1)
2024-05-09 19:57:27 +02:00
expect(sheet.setHeaderRow).toHaveBeenCalledWith([
"name",
"description",
"location",
])
})
})
2023-05-23 10:14:06 +02:00
describe("getTableNames", () => {
it("can fetch table names", async () => {
await config.doInContext(structures.uuid(), async () => {
const sheetNames: string[] = []
for (let i = 0; i < 5; i++) {
const sheet = createSheet({ headerValues: [] })
sheetsByIndex.push(sheet)
sheetNames.push(sheet.title)
}
const res = await integration.getTableNames()
2023-05-23 10:17:42 +02:00
2024-03-19 10:46:10 +01:00
expect(mockGoogleIntegration.loadInfo).toHaveBeenCalledTimes(1)
2023-05-23 10:14:06 +02:00
expect(res).toEqual(sheetNames)
})
})
})
2023-05-23 10:22:26 +02:00
describe("testConnection", () => {
it("can test successful connections", async () => {
await config.doInContext(structures.uuid(), async () => {
const res = await integration.testConnection()
2024-03-19 10:46:10 +01:00
expect(mockGoogleIntegration.loadInfo).toHaveBeenCalledTimes(1)
2023-05-23 10:22:26 +02:00
expect(res).toEqual({ connected: true })
})
})
})
})