-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eb5ccbb
commit 371f327
Showing
43 changed files
with
442 additions
and
76 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
/* eslint-disable */ | ||
|
||
const { request } = require("@playwright/test"); | ||
const dotenv = require("dotenv"); | ||
const process = require("process"); | ||
const crypto = require("crypto"); | ||
const path = require("path"); | ||
const fs = require("fs"); | ||
|
||
dotenv.config(); | ||
|
||
const PERMISSIONS = [ | ||
"admin", | ||
"app", | ||
"discount", | ||
"order", | ||
"channel", | ||
"customer", | ||
"giftCard", | ||
"page", | ||
"plugin", | ||
"productTypeAndAttribute", | ||
"product", | ||
"settings", | ||
"staff", | ||
"shipping", | ||
"translations", | ||
] | ||
|
||
const ACCOUNT_EMAILS = { | ||
channel: "[email protected]", | ||
shipping: "[email protected]", | ||
giftCard: "[email protected]", | ||
app: "[email protected]", | ||
settings: "[email protected]", | ||
page: "[email protected]", | ||
order: "[email protected]", | ||
translations: "[email protected]", | ||
staff: "[email protected]", | ||
customer: "[email protected]", | ||
productTypeAndAttribute: "[email protected]", | ||
discount: "[email protected]", | ||
plugin: "[email protected]", | ||
product: "[email protected]", | ||
}; | ||
|
||
const createQuery = (email, password) => `mutation TokenAuth{ | ||
tokenCreate(email: "${email}", password: "${password}") { | ||
token | ||
refreshToken | ||
errors: errors { | ||
code | ||
message | ||
} | ||
user { | ||
id | ||
} | ||
} | ||
}`; | ||
|
||
|
||
const getEmailForPermission = (permission) => { | ||
if (permission === "admin") { | ||
return process.env.E2E_USER_NAME; | ||
} | ||
|
||
return ACCOUNT_EMAILS[permission]; | ||
}; | ||
|
||
const getPasswordForPermission = (permission) => { | ||
if (permission === "admin") { | ||
return process.env.E2E_USER_PASSWORD; | ||
} | ||
|
||
return process.env.E2E_PERMISSIONS_USERS_PASSWORD; | ||
}; | ||
|
||
const getAuthForPermission = async (permissionName) => { | ||
const apiRequestContext = await request.newContext({ | ||
baseURL: process.env.BASE_URL, | ||
}); | ||
|
||
const email = getEmailForPermission(permissionName); | ||
const password = getPasswordForPermission(permissionName); | ||
const query = createQuery(email, password) | ||
|
||
const response = await apiRequestContext.post(process.env.API_URL || "", { | ||
data: { query }, | ||
}); | ||
const resposnseObj = await response.json() | ||
const { errors } = resposnseObj.data.tokenCreate | ||
|
||
if (errors && errors.length > 0) { | ||
const errorMessages = errors | ||
.map(e => e.message) | ||
.join(", "); | ||
|
||
throw new Error(`Login failed for permission ${permissionName}: ${errorMessages}`); | ||
} | ||
|
||
const loginJsonInfo = await apiRequestContext.storageState(); | ||
|
||
return loginJsonInfo | ||
} | ||
|
||
const encrypt = (password, text) => { | ||
const algorithm = 'aes-256-ctr'; | ||
const key = Buffer.concat([Buffer.from(password), Buffer.alloc(32)], 32); | ||
const iv = crypto.randomBytes(16); | ||
const cipher = crypto.createCipheriv(algorithm, key, iv); | ||
let encrypted = cipher.update(text); | ||
encrypted = Buffer.concat([encrypted, cipher.final()]); | ||
return iv.toString('hex') + encrypted.toString('hex'); | ||
} | ||
|
||
const decrypt = (password, text) => { | ||
const algorithm = 'aes-256-ctr'; | ||
const key = Buffer.concat([Buffer.from(password), Buffer.alloc(32)], 32); | ||
const iv = Buffer.from(text.substring(0, 32), 'hex'); | ||
const encryptedText = Buffer.from(text.substring(32), 'hex'); | ||
const decipher = crypto.createDecipheriv(algorithm, key, iv); | ||
let decrypted = decipher.update(encryptedText); | ||
decrypted = Buffer.concat([decrypted, decipher.final()]); | ||
return decrypted.toString(); | ||
} | ||
|
||
(async () => { | ||
const command = process.argv[2] | ||
|
||
if (command == "login") { | ||
let authString = '' | ||
|
||
for (const permissionName of PERMISSIONS) { | ||
|
||
const auth = await getAuthForPermission(permissionName) | ||
|
||
|
||
authString = `${authString}${JSON.stringify(auth)}|` | ||
} | ||
|
||
const encodedString = encrypt(process.env.E2E_ENCODE_PASS, authString) | ||
|
||
process.stdout.write(encodedString) | ||
} | ||
|
||
|
||
if (command == "restore") { | ||
const tempDir = path.join(__dirname, "../playwright/.auth"); | ||
|
||
if (!fs.existsSync(tempDir)) { | ||
fs.mkdirSync(tempDir, { recursive: true }); | ||
} | ||
|
||
|
||
const encodedInput = process.argv[3] | ||
console.log("Restoring...", encodedInput.length) | ||
|
||
|
||
const decodedString = decrypt(process.env.E2E_ENCODE_PASS, encodedInput) | ||
decodedString | ||
.split("|") | ||
.filter(Boolean) | ||
.map(JSON.parse) | ||
.map(obj => { | ||
obj.origins.push({ | ||
origin: process.env.BASE_URL, | ||
localStorage: [ | ||
{ | ||
name: "_saleorRefreshToken", | ||
value: obj.cookies[0].value, | ||
}, | ||
], | ||
}); | ||
|
||
return obj | ||
}) | ||
.map(JSON.stringify) | ||
.map((str, index) => ({ name: PERMISSIONS[index], str })) | ||
.forEach(token => { | ||
const storageStatePath = path.join(tempDir, `${token.name}.json`); | ||
fs.writeFileSync(storageStatePath, token.str); | ||
}) | ||
} | ||
})() |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,7 +32,9 @@ export const USER_PERMISSION: UserPermissionType = { | |
product: "[email protected]", | ||
}; | ||
|
||
export const permissions: Array<keyof UserPermissionType> = [ | ||
export type UserPermission = keyof UserPermissionType; | ||
|
||
export const permissions: Array<UserPermission> = [ | ||
"app", | ||
"discount", | ||
"order", | ||
|
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
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
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
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
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
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
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
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
Oops, something went wrong.