2023-02-27 17:25:48 +01:00
|
|
|
import type { GoogleSpreadsheetWorksheet } from "google-spreadsheet"
|
|
|
|
|
|
|
|
jest.mock("google-auth-library")
|
|
|
|
const { OAuth2Client } = require("google-auth-library")
|
|
|
|
|
|
|
|
const setCredentialsMock = jest.fn()
|
|
|
|
const getAccessTokenMock = jest.fn()
|
|
|
|
|
|
|
|
OAuth2Client.mockImplementation(() => {
|
|
|
|
return {
|
|
|
|
setCredentials: setCredentialsMock,
|
|
|
|
getAccessToken: getAccessTokenMock,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
jest.mock("google-spreadsheet")
|
|
|
|
const { GoogleSpreadsheet } = require("google-spreadsheet")
|
|
|
|
|
|
|
|
const sheetsByTitle: { [title: string]: GoogleSpreadsheetWorksheet } = {}
|
2023-05-23 10:14:06 +02:00
|
|
|
const sheetsByIndex: GoogleSpreadsheetWorksheet[] = []
|
2023-05-23 10:17:42 +02:00
|
|
|
const mockGoogleIntegration = {
|
|
|
|
useOAuth2Client: jest.fn(),
|
|
|
|
loadInfo: jest.fn(),
|
|
|
|
sheetsByTitle,
|
|
|
|
sheetsByIndex,
|
|
|
|
}
|
2023-02-27 17:25:48 +01:00
|
|
|
|
2023-05-23 10:17:42 +02:00
|
|
|
GoogleSpreadsheet.mockImplementation(() => mockGoogleIntegration)
|
2023-02-27 17:25:48 +01:00
|
|
|
|
|
|
|
import { structures } from "@budibase/backend-core/tests"
|
|
|
|
import TestConfiguration from "../../tests/utilities/TestConfiguration"
|
|
|
|
import GoogleSheetsIntegration from "../googlesheets"
|
2023-10-26 14:19:09 +02:00
|
|
|
import { FieldType, Table, TableSchema, TableSourceType } from "@budibase/types"
|
2023-10-25 20:00:25 +02:00
|
|
|
import { generateDatasourceID } from "../../db/utils"
|
2023-02-27 17:25:48 +01:00
|
|
|
|
|
|
|
describe("Google Sheets Integration", () => {
|
|
|
|
let integration: any,
|
|
|
|
config = new TestConfiguration()
|
2023-10-30 17:46:27 +01:00
|
|
|
let cleanupEnv: () => void
|
2023-02-27 17:25:48 +01:00
|
|
|
|
2023-03-06 11:33:49 +01:00
|
|
|
beforeAll(() => {
|
2023-12-11 18:43:56 +01:00
|
|
|
cleanupEnv = config.setCoreEnv({
|
2023-10-30 17:46:27 +01:00
|
|
|
GOOGLE_CLIENT_ID: "test",
|
|
|
|
GOOGLE_CLIENT_SECRET: "test",
|
|
|
|
})
|
2023-03-06 11:33:49 +01:00
|
|
|
})
|
|
|
|
|
2023-03-27 20:38:49 +02:00
|
|
|
afterAll(async () => {
|
2023-10-30 17:46:27 +01:00
|
|
|
cleanupEnv()
|
|
|
|
config.end()
|
2023-03-27 20:38:49 +02:00
|
|
|
})
|
|
|
|
|
2023-02-27 17:25:48 +01:00
|
|
|
beforeEach(async () => {
|
|
|
|
integration = new GoogleSheetsIntegration.integration({
|
|
|
|
spreadsheetId: "randomId",
|
|
|
|
auth: {
|
|
|
|
appId: "appId",
|
|
|
|
accessToken: "accessToken",
|
|
|
|
refreshToken: "refreshToken",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
await config.init()
|
2023-05-23 10:17:42 +02:00
|
|
|
|
|
|
|
jest.clearAllMocks()
|
2023-02-27 17:25:48 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
function createBasicTable(name: string, columns: string[]): Table {
|
|
|
|
return {
|
2023-10-26 14:19:09 +02:00
|
|
|
type: "table",
|
2023-02-27 17:25:48 +01:00
|
|
|
name,
|
2023-10-25 20:00:25 +02:00
|
|
|
sourceId: generateDatasourceID(),
|
2023-10-26 14:19:09 +02:00
|
|
|
sourceType: TableSourceType.EXTERNAL,
|
2023-02-27 17:25:48 +01:00
|
|
|
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 () => {
|
2023-02-27 17:25:48 +01:00
|
|
|
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-02-27 17:25:48 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
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 () => {
|
2023-02-27 17:25:48 +01:00
|
|
|
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
|
2023-02-27 17:25:48 +01:00
|
|
|
})
|
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-02-27 17:25:48 +01:00
|
|
|
})
|
|
|
|
})
|
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 })
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2023-02-27 17:25:48 +01:00
|
|
|
})
|