Skip to content

Commit 005a305

Browse files
committedAug 25, 2023
fix: non-working e2e tests + refactoring
1 parent ab9aef6 commit 005a305

File tree

12 files changed

+219
-175
lines changed

12 files changed

+219
-175
lines changed
 
File renamed without changes.

‎src/gas/examples/appsscript.json

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
"oauthScopes": [
1515
"https://mail.google.com/",
1616
"https://www.googleapis.com/auth/drive",
17+
"https://www.googleapis.com/auth/script.external_request",
18+
"https://www.googleapis.com/auth/script.send_mail",
1719
"https://www.googleapis.com/auth/spreadsheets",
1820
"https://www.googleapis.com/auth/userinfo.email"
1921
],

‎src/gas/examples/init-e2e-tests.ts renamed to ‎src/gas/examples/e2eInit.ts

+18-10
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
declare namespace GmailProcessorLib {
2-
type E2EConfig = any
3-
}
4-
51
declare const GmailProcessorLib: {
6-
E2EInit(config: any)
2+
E2E: any
73
}
84

9-
const e2eConfig: GmailProcessorLib.E2EConfig = {
5+
const e2eConfig = {
106
globals: {
117
repoBaseUrl:
128
"https://raw.githubusercontent.com/ahochsteger/gmail-processor/main/src/e2e-test/files",
@@ -16,7 +12,7 @@ const e2eConfig: GmailProcessorLib.E2EConfig = {
1612
folders: [
1713
{
1814
name: "e2e",
19-
location: "{id:1yVPXknT_gIdB6-jJdGF2u3mQR6en4dGy}/e2e",
15+
location: "/GmailProcessor-Tests/e2e",
2016
},
2117
],
2218
files: [
@@ -44,14 +40,26 @@ const e2eConfig: GmailProcessorLib.E2EConfig = {
4440
],
4541
mails: [
4642
{
47-
name: "01-multiple",
48-
subject: "Test Mail with attachments",
43+
name: "01-single-attachment",
44+
subject: "Test with a single attachments",
45+
htmlBody: "Test email with a single attachment from the github repo.",
46+
files: ["plaintext-repo"],
47+
},
48+
{
49+
name: "02-multiple-attachments",
50+
subject: "Test with multiple attachments",
4951
htmlBody: "Test email with multiple attachments from different sources.",
5052
files: ["gmail-logo", "plaintext-drive", "plaintext-repo"],
5153
},
54+
{
55+
name: "01-multiple",
56+
subject: "Test with no attachments",
57+
htmlBody: "Test email with no attachments.",
58+
files: [],
59+
},
5260
],
5361
}
5462

5563
function e2eInit() {
56-
GmailProcessorLib.E2EInit(e2eConfig)
64+
GmailProcessorLib.E2E.initAll(e2eConfig)
5765
}

‎src/gas/examples/e2e01.js renamed to ‎src/gas/examples/e2eTest01.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const e2e01ConfigV2 = {
1+
const e2eTest01ConfigV2 = {
22
description: "End-to-end (E2E) test configuration",
33
settings: {
44
logSheetLocation:
@@ -76,6 +76,6 @@ const e2e01ConfigV2 = {
7676
],
7777
}
7878

79-
function e2e01Run() {
80-
GmailProcessorLib.run(e2e01ConfigV2, "dry-run")
79+
function e2eTest01Run() {
80+
GmailProcessorLib.run(e2eTest01ConfigV2, "dry-run")
8181
}

‎src/gas/lib/appsscript.json

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
"oauthScopes": [
55
"https://mail.google.com/",
66
"https://www.googleapis.com/auth/drive",
7+
"https://www.googleapis.com/auth/script.external_request",
8+
"https://www.googleapis.com/auth/script.send_mail",
79
"https://www.googleapis.com/auth/spreadsheets",
810
"https://www.googleapis.com/auth/userinfo.email"
911
],

‎src/lib/Context.ts

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ export type MetaInfo = {
7272
type EnvInfo = {
7373
gmailApp: GoogleAppsScript.Gmail.GmailApp
7474
gdriveApp: GoogleAppsScript.Drive.DriveApp
75+
mailApp: GoogleAppsScript.Mail.MailApp
7576
utilities: GoogleAppsScript.Utilities.Utilities
7677
spreadsheetApp: GoogleAppsScript.Spreadsheet.SpreadsheetApp
7778
cacheService: GoogleAppsScript.Cache.CacheService

‎src/lib/EnvProvider.ts

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import {
2+
ContextType,
3+
EnvContext,
4+
MetaInfoType as MIT,
5+
MetaInfo,
6+
RunMode,
7+
newMetaInfo as mi,
8+
} from "./Context"
9+
import { Logger as Log } from "./utils/Logger"
10+
11+
export class EnvProvider {
12+
public static buildMetaInfo(ctx: EnvContext) {
13+
const m: MetaInfo = {
14+
"date.now": mi(MIT.DATE, () => new Date(), "The current timestamp."),
15+
"env.runMode": mi(
16+
MIT.STRING,
17+
() => ctx.env.runMode,
18+
"The runmode used for processing.",
19+
),
20+
"env.timezone": mi(
21+
MIT.STRING,
22+
() => ctx.env.timezone,
23+
"The timezone used for processing.",
24+
),
25+
"user.email": mi(
26+
MIT.STRING,
27+
() => ctx.env.session.getActiveUser().getEmail(),
28+
"The email address of the active user.",
29+
),
30+
}
31+
return m
32+
}
33+
34+
public static defaultContext(runMode = RunMode.SAFE_MODE) {
35+
const logger = new Log()
36+
const ctx: EnvContext = {
37+
type: ContextType.ENV,
38+
env: {
39+
cacheService: CacheService,
40+
gdriveApp: DriveApp,
41+
gmailApp: GmailApp,
42+
mailApp: MailApp,
43+
spreadsheetApp: SpreadsheetApp,
44+
utilities: Utilities,
45+
runMode: runMode,
46+
session: Session,
47+
timezone: Session?.getScriptTimeZone() || "UTC",
48+
},
49+
envMeta: {},
50+
log: logger,
51+
meta: {},
52+
}
53+
ctx.envMeta = this.buildMetaInfo(ctx)
54+
ctx.meta = { ...ctx.envMeta }
55+
return ctx
56+
}
57+
}

‎src/lib/e2e/E2E.ts

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import { EnvContext, RunMode } from "../Context"
2+
import { EnvProvider } from "../EnvProvider"
3+
import {
4+
ConflictStrategy,
5+
DriveUtils,
6+
FileContent,
7+
} from "../adapter/GDriveAdapter"
8+
9+
export type FileConfig = {
10+
name: string
11+
type: string
12+
filename: string
13+
ref: string
14+
destFolder: string
15+
}
16+
17+
export type E2EConfig = {
18+
globals: {
19+
repoBaseUrl: string
20+
subjectPrefix: string
21+
to: string
22+
}
23+
folders: {
24+
name: string
25+
location: string
26+
}[]
27+
files: FileConfig[]
28+
mails: {
29+
name: string
30+
subject: string
31+
htmlBody: string
32+
files: string[]
33+
}[]
34+
}
35+
36+
export class E2E {
37+
public static getBlobFromFileEntry(
38+
config: E2EConfig,
39+
file: FileConfig,
40+
): GoogleAppsScript.Base.Blob | undefined {
41+
let blob
42+
switch (file.type) {
43+
case "repo": {
44+
const url = `${config.globals.repoBaseUrl}/${file.ref}`
45+
console.log(`Fetching repo file from ${url} ...`)
46+
blob = UrlFetchApp.fetch(url).getBlob()
47+
break
48+
}
49+
case "url":
50+
console.log(`Fetching URL file from ${file.ref} ...`)
51+
blob = UrlFetchApp.fetch(file.ref).getBlob()
52+
break
53+
case "gdrive":
54+
console.log(`Fetching GDrive file from ${file.ref} ...`)
55+
blob = DriveApp.getFileById(file.ref).getBlob()
56+
break
57+
}
58+
return blob
59+
}
60+
61+
public static initMails(ctx: EnvContext, config: E2EConfig) {
62+
ctx.log.info("GMail initialization started.")
63+
config.mails.forEach((mail) => {
64+
const files: string[] = mail.files ?? []
65+
const attachments: GoogleAppsScript.Base.Blob[] = files.map((name) => {
66+
const file = config.files.reduce((prev, curr) =>
67+
name === curr.name ? curr : prev,
68+
)
69+
return this.getBlobFromFileEntry(config, file)
70+
}) as GoogleAppsScript.Base.Blob[]
71+
ctx.env.mailApp.sendEmail({
72+
to: config.globals.to,
73+
subject: `${config.globals.subjectPrefix}${mail.subject}`,
74+
htmlBody: mail.htmlBody,
75+
attachments: attachments,
76+
})
77+
})
78+
ctx.log.info("GMail initialization finished.")
79+
}
80+
81+
public static initDrive(ctx: EnvContext, config: E2EConfig) {
82+
ctx.log.info("GDrive initialization started.")
83+
config.files
84+
.filter((file) => file.destFolder !== undefined)
85+
.forEach((file) => {
86+
const blob = this.getBlobFromFileEntry(config, file)
87+
if (!blob) return
88+
const folderLocation = config.folders.reduce((prev, current) =>
89+
current.name === file.destFolder ? current : prev,
90+
)
91+
DriveUtils.createFile(
92+
ctx,
93+
`${folderLocation.location}/${file.filename}`,
94+
new FileContent(blob),
95+
ConflictStrategy.REPLACE,
96+
)
97+
})
98+
ctx.log.info("GDrive initialization finished.")
99+
}
100+
101+
public static initAll(
102+
config: E2EConfig,
103+
ctx: EnvContext = EnvProvider.defaultContext(RunMode.DANGEROUS),
104+
) {
105+
ctx.log.info("E2E initialization started.")
106+
this.initDrive(ctx, config)
107+
this.initMails(ctx, config)
108+
ctx.log.info("E2E initialization finished.")
109+
}
110+
}

0 commit comments

Comments
 (0)
Please sign in to comment.