Last active
June 3, 2020 08:29
-
-
Save NilukaSripalim/ee9efd3e5f1be60326e172a4c5c66783 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <reference types="cypress" /> | |
// *********************************************************** | |
// This example plugins/index.js can be used to load plugins | |
// | |
// You can change the location of this file or turn off loading | |
// the plugins file with the 'pluginsFile' configuration option. | |
// | |
// You can read more here: | |
// https://on.cypress.io/plugins-guide | |
// *********************************************************** | |
// This function is called when a project is opened or re-opened (e.g. due to | |
// the project's config changing) | |
const { promisify } = require("util"); | |
const { google } = require("googleapis"); | |
const { OAuth2Client } = require("google-auth-library"); | |
const fs = require("fs"); | |
const pify = require("pify"); | |
var unescape = require("lodash.unescape"); | |
const gmail = google.gmail("v1"); | |
const gmailGetMessagesAsync = promisify(gmail.users.messages.get); | |
const gmailListMessagesAsync = promisify(gmail.users.messages.list); | |
// Promisify with promise | |
const readFileAsync = promisify(fs.readFile); | |
// Gmail label list | |
const gmailListLabesAsync = promisify(gmail.users.labels.list); | |
const TOKEN_DIR = __dirname; | |
const TOKEN_PATH = TOKEN_DIR + "/gmail-nodejs-quickstart.json"; | |
/** | |
* @type {Cypress.PluginConfig} | |
*/ | |
module.exports = (on, config) => { | |
// `on` is used to hook into various events Cypress emits | |
// `config` is the resolved Cypress config | |
on("task", { | |
"gmail:get-messages": async () => { | |
// Get credential information | |
let confirm_msg; | |
const content = await readFileAsync(__dirname + "/client_secret.json"); | |
// specify the client secret file | |
const credentials = JSON.parse(content); | |
// credential | |
// authentication | |
const clientSecret = credentials.installed.client_secret; | |
const clientId = credentials.installed.client_id; | |
const redirectUrl = credentials.installed.redirect_uris[0]; | |
const oauth2Client = new OAuth2Client( | |
clientId, | |
clientSecret, | |
redirectUrl | |
); | |
const token = await readFileAsync(TOKEN_PATH); | |
oauth2Client.credentials = JSON.parse(token); | |
let res = await gmailListMessagesAsync({ | |
auth: oauth2Client, | |
userId: "me", | |
maxResults: 1, | |
}); | |
const newestMessageId = res["data"]["messages"][0]["id"]; | |
res = await gmailGetMessagesAsync({ | |
auth: oauth2Client, | |
userId: "me", | |
id: newestMessageId, | |
}); | |
let body_content = JSON.stringify(res.data.payload.body.data); | |
let data, buff, text; | |
try { | |
data = body_content; | |
buff = new Buffer.from(data, "base64"); | |
text = buff.toString(); | |
let regex = /https?:\/\/[\-A-Za-z0-9+&@#\/%?=~_|$!:,.;]*/g; | |
let filtered = text.match(regex); | |
filtered = filtered.filter( | |
(item) => !item.includes("www") && item.length > 500 | |
); | |
filtered = Array.from(filtered); | |
filtered = filtered.shift(); | |
confirm_msg= unescape(filtered); | |
} catch (err) { | |
console.log("error"); | |
data = body_content; | |
buff = new Buffer.from(data, "base64"); | |
text = buff.toString(); | |
let regex = /https?:\/\/[\-A-Za-z0-9+&@#\/%?=~_|$!:,.;]*/g; | |
let filtered = text.match(regex); | |
filtered = filtered.filter( | |
(item) => !item.includes("www") && item.length > 500 | |
); | |
filtered = Array.from(filtered); | |
filtered = filtered.shift(); | |
confirm_msg = unescape(filtered); | |
} | |
return confirm_msg; | |
}, | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Read gmail api with cypress