2021-12-07 12:51:08 +03:00
// Set of common function that should be main building blocks for e2e tests.
// The e2e tests usually consist of following building blocks:
// - request agent
// - state builder
2021-12-07 19:21:48 +03:00
// - output state checker (in case we don't get jest snapshots working)
2021-12-07 12:51:08 +03:00
//
2022-08-30 11:39:46 +03:00
// The request agent is responsible for making HTTP-like requests to an application (express app in case of Ghost).
2021-12-07 12:51:08 +03:00
// Note there's no actual need to make an HTTP request to an actual server, bypassing HTTP and hooking into the application
// directly is enough and reduces dependence on blocking a port (allows to run tests in parallel).
//
// The state builder is responsible for building the state of the application. Usually it's done by using pre-defined fixtures.
// Can include building a DB state, file system state (themes, config files), building configuration state (config files) etc.
//
// The output state checker is responsible for checking the response from the app after performing a request.
2021-12-07 19:21:48 +03:00
const _ = require ( 'lodash' ) ;
2023-06-12 11:56:15 +03:00
const debug = require ( '@tryghost/debug' ) ( 'test' ) ;
2021-12-07 19:21:48 +03:00
const { sequence } = require ( '@tryghost/promise' ) ;
2022-02-22 05:41:50 +03:00
const { any , stringMatching } = require ( '@tryghost/express-test' ) . snapshot ;
2022-12-02 11:44:32 +03:00
const { AsymmetricMatcher } = require ( 'expect' ) ;
2021-12-09 11:48:01 +03:00
const fs = require ( 'fs-extra' ) ;
const path = require ( 'path' ) ;
const os = require ( 'os' ) ;
const uuid = require ( 'uuid' ) ;
2021-12-09 14:10:06 +03:00
2022-02-08 20:13:30 +03:00
const fixtureUtils = require ( './fixture-utils' ) ;
2021-12-09 11:48:01 +03:00
const redirectsUtils = require ( './redirects' ) ;
const configUtils = require ( './configUtils' ) ;
2022-02-11 16:42:35 +03:00
const urlServiceUtils = require ( './url-service-utils' ) ;
2022-02-08 19:52:38 +03:00
const mockManager = require ( './e2e-framework-mock-manager' ) ;
2023-02-22 20:15:53 +03:00
const mentionsJobsService = require ( '../../core/server/services/mentions-jobs' ) ;
const jobsService = require ( '../../core/server/services/jobs' ) ;
2021-12-07 12:51:08 +03:00
const boot = require ( '../../core/boot' ) ;
2022-07-26 19:00:37 +03:00
const { AdminAPITestAgent , ContentAPITestAgent , GhostAPITestAgent , MembersAPITestAgent } = require ( './agents' ) ;
2021-12-07 19:21:48 +03:00
const db = require ( './db-utils' ) ;
2021-12-07 12:51:08 +03:00
2022-02-11 19:19:07 +03:00
// Services that need resetting
2022-05-10 14:55:58 +03:00
const settingsService = require ( '../../core/server/services/settings/settings-service' ) ;
2022-09-27 19:44:20 +03:00
const supertest = require ( 'supertest' ) ;
Fixed configUtils and adapter cache issues in E2E tests (#16167)
no issue
There are a couple of issues with resetting the Ghost instance between
E2E test files:
These issues came to the surface because of new tests written in
https://github.com/TryGhost/Ghost/pull/16117
**1. configUtils.restore does not work correctly**
`config.reset()` is a callback based method. On top of that, it doesn't
really work reliably (https://github.com/indexzero/nconf/issues/93)
What kinda happens, is that you first call `config.reset` but
immediately after you correcty reset the config using the `config.set`
calls afterwards. But since `config.reset` is async, that reset will
happen after all those sets, and the end result is that it isn't reset
correctly.
This mainly caused issues in the new updated images tests, which were
updating the config `imageOptimization.contentImageSizes`, which is a
deeply nested config value. Maybe some references to objects are reused
in nconf that cause this issue?
Wrapping `config.reset()` in a promise does fix the issue.
**2. Adapters cache not reset between tests**
At the start of each test, we set `paths:contentPath` to a nice new
temporary directory. But if a previous test already requests a
localStorage adapter, that adapter would have been created and in the
constructor `paths:contentPath` would have been passed. That same
instance will be reused in the next test run. So it won't read the new
config again. To fix this, we need to reset the adapter instances
between E2E tests.
How was this visible? Test uploads were stored in the actual git
repository, and not in a temporary directory. When writing the new image
upload tests, this also resulted in unreliable test runs because some
image names were already taken (from previous test runs).
**3. Old 2E2 test Ghost server not stopped**
Sometimes we still need access to the frontend test server using
`getAgentsWithFrontend`. But that does start a new Ghost server which is
actually listening for HTTP traffic. This could result in a fatal error
in tests because the port is already in use. The issue is that old E2E
tests also start a HTTP server, but they don't stop the server. When you
used the old `startGhost` util, it would check if a server was already
running and stop it first. The new `getAgentsWithFrontend` now also has
the same functionality to fix that issue.
2023-01-30 16:06:20 +03:00
const { stopGhost } = require ( './e2e-utils' ) ;
const adapterManager = require ( '../../core/server/services/adapter-manager' ) ;
2023-02-22 20:15:53 +03:00
const DomainEvents = require ( '@tryghost/domain-events' ) ;
2022-02-11 19:19:07 +03:00
2023-03-14 18:03:57 +03:00
// Require additional assertions which help us keep our tests small and clear
require ( './assertions' ) ;
2023-06-12 11:56:15 +03:00
let totalResetTime = 0 ;
let totalStartTime = 0 ;
let totalBoots = 0 ;
2022-02-11 15:32:08 +03:00
/ * *
* @ param { Object } [ options = { } ]
* @ param { Boolean } [ options . backend ] Boot the backend
* @ param { Boolean } [ options . frontend ] Boot the frontend
* @ param { Boolean } [ options . server ] Start a server
2022-02-11 16:42:35 +03:00
* @ returns { Promise < Express . Application > } ghost
2022-02-11 15:32:08 +03:00
* /
const startGhost = async ( options = { } ) => {
2023-02-22 20:15:53 +03:00
await mentionsJobsService . allSettled ( ) ;
await jobsService . allSettled ( ) ;
await DomainEvents . allSettled ( ) ;
2021-12-09 11:48:01 +03:00
/ * *
2022-02-11 14:08:28 +03:00
* We never use the root content folder for testing !
* We use a tmp folder .
* /
2021-12-09 11:48:01 +03:00
const contentFolder = path . join ( os . tmpdir ( ) , uuid . v4 ( ) , 'ghost-test' ) ;
await prepareContentFolder ( { contentFolder } ) ;
// NOTE: need to pass this config to the server instance
configUtils . set ( 'paths:contentPath' , contentFolder ) ;
Fixed configUtils and adapter cache issues in E2E tests (#16167)
no issue
There are a couple of issues with resetting the Ghost instance between
E2E test files:
These issues came to the surface because of new tests written in
https://github.com/TryGhost/Ghost/pull/16117
**1. configUtils.restore does not work correctly**
`config.reset()` is a callback based method. On top of that, it doesn't
really work reliably (https://github.com/indexzero/nconf/issues/93)
What kinda happens, is that you first call `config.reset` but
immediately after you correcty reset the config using the `config.set`
calls afterwards. But since `config.reset` is async, that reset will
happen after all those sets, and the end result is that it isn't reset
correctly.
This mainly caused issues in the new updated images tests, which were
updating the config `imageOptimization.contentImageSizes`, which is a
deeply nested config value. Maybe some references to objects are reused
in nconf that cause this issue?
Wrapping `config.reset()` in a promise does fix the issue.
**2. Adapters cache not reset between tests**
At the start of each test, we set `paths:contentPath` to a nice new
temporary directory. But if a previous test already requests a
localStorage adapter, that adapter would have been created and in the
constructor `paths:contentPath` would have been passed. That same
instance will be reused in the next test run. So it won't read the new
config again. To fix this, we need to reset the adapter instances
between E2E tests.
How was this visible? Test uploads were stored in the actual git
repository, and not in a temporary directory. When writing the new image
upload tests, this also resulted in unreliable test runs because some
image names were already taken (from previous test runs).
**3. Old 2E2 test Ghost server not stopped**
Sometimes we still need access to the frontend test server using
`getAgentsWithFrontend`. But that does start a new Ghost server which is
actually listening for HTTP traffic. This could result in a fatal error
in tests because the port is already in use. The issue is that old E2E
tests also start a HTTP server, but they don't stop the server. When you
used the old `startGhost` util, it would check if a server was already
running and stop it first. The new `getAgentsWithFrontend` now also has
the same functionality to fix that issue.
2023-01-30 16:06:20 +03:00
// Adapter cache has to be cleared to avoid reusing cached adapter instances between restarts
adapterManager . clearCache ( ) ;
2023-06-13 13:33:47 +03:00
// Reset the URL service so we clear out all the listeners
urlServiceUtils . resetGenerators ( ) ;
2021-12-07 12:51:08 +03:00
const defaults = {
backend : true ,
frontend : false ,
2022-12-08 15:49:38 +03:00
server : false
2021-12-07 12:51:08 +03:00
} ;
2022-02-11 19:19:07 +03:00
// Ensure the state of all data, including DB and caches
2023-06-12 11:56:15 +03:00
const resetDataNow = Date . now ( ) ;
2022-02-11 19:19:07 +03:00
await resetData ( ) ;
2023-06-12 11:56:15 +03:00
totalResetTime += Date . now ( ) - resetDataNow ;
2022-02-07 18:43:25 +03:00
2022-02-11 16:42:35 +03:00
const bootOptions = Object . assign ( { } , defaults , options ) ;
2023-06-12 11:56:15 +03:00
const bootNow = Date . now ( ) ;
2022-02-11 16:42:35 +03:00
const ghostServer = await boot ( bootOptions ) ;
2023-06-12 11:56:15 +03:00
const bootTime = Date . now ( ) - bootNow ;
totalStartTime += bootTime ;
totalBoots += 1 ;
2022-02-11 16:42:35 +03:00
if ( bootOptions . frontend ) {
await urlServiceUtils . isFinished ( ) ;
}
2023-03-07 15:20:28 +03:00
// Disable network in tests at the start
mockManager . disableNetwork ( ) ;
2023-06-12 11:56:15 +03:00
debug ( ` [e2e-framework] Started Ghost in ${ bootTime / 1000 } s ` ) ;
debug ( ` [e2e-framework] Accumulated start time across ${ totalBoots } boots is ${ totalStartTime / 1000 } s (average = ${ Math . round ( totalStartTime / totalBoots ) } ms) ` ) ;
debug ( ` [e2e-framework] Accumulated reset time across ${ totalBoots } boots is ${ totalResetTime / 1000 } s (average = ${ Math . round ( totalResetTime / totalBoots ) } ms) ` ) ;
2022-02-11 16:42:35 +03:00
return ghostServer ;
2021-12-07 12:51:08 +03:00
} ;
2021-12-09 11:48:01 +03:00
/ * *
* Slightly simplified copy - paste from e2e - utils .
* @ param { Object } options
* /
2022-07-26 18:28:16 +03:00
const prepareContentFolder = async ( { contentFolder , redirectsFile = true , routesFile = true } ) => {
2021-12-09 11:48:01 +03:00
const contentFolderForTests = contentFolder ;
2022-07-26 18:28:16 +03:00
await fs . ensureDir ( contentFolderForTests ) ;
await fs . ensureDir ( path . join ( contentFolderForTests , 'data' ) ) ;
await fs . ensureDir ( path . join ( contentFolderForTests , 'themes' ) ) ;
await fs . ensureDir ( path . join ( contentFolderForTests , 'images' ) ) ;
await fs . ensureDir ( path . join ( contentFolderForTests , 'logs' ) ) ;
await fs . ensureDir ( path . join ( contentFolderForTests , 'adapters' ) ) ;
await fs . ensureDir ( path . join ( contentFolderForTests , 'settings' ) ) ;
2021-12-09 11:48:01 +03:00
2023-09-06 23:22:12 +03:00
// Copy all themes into the new test content folder. Default active theme is always source.
2021-12-09 11:48:01 +03:00
// If you want to use a different theme, you have to set the active theme (e.g. stub)
2022-07-26 18:28:16 +03:00
await fs . copy (
2021-12-09 11:48:01 +03:00
path . join ( _ _dirname , 'fixtures' , 'themes' ) ,
path . join ( contentFolderForTests , 'themes' )
) ;
if ( redirectsFile ) {
redirectsUtils . setupFile ( contentFolderForTests , '.yaml' ) ;
}
if ( routesFile ) {
2022-07-26 18:28:16 +03:00
await fs . copy (
2021-12-09 11:48:01 +03:00
path . join ( _ _dirname , 'fixtures' , 'settings' , 'routes.yaml' ) ,
path . join ( contentFolderForTests , 'settings' , 'routes.yaml' )
) ;
}
} ;
2021-12-07 19:21:48 +03:00
/ * *
* Database state builder . By default inserts an owner user into the database .
* @ param { ... any } [ options ]
* @ returns { Promise < void > }
* /
const initFixtures = async ( ... options ) => {
// No DB setup, but override the owner
options = _ . merge ( { 'owner:post' : true } , _ . transform ( options , function ( result , val ) {
if ( val ) {
result [ val ] = true ;
}
} ) ) ;
2022-02-08 20:13:30 +03:00
const fixtureOps = fixtureUtils . getFixtureOps ( options ) ;
2021-12-07 19:21:48 +03:00
return sequence ( fixtureOps ) ;
} ;
const getFixture = ( type , index = 0 ) => {
2022-02-08 20:13:30 +03:00
return fixtureUtils . DataGenerator . forKnex [ type ] [ index ] ;
2021-12-07 19:21:48 +03:00
} ;
2022-10-05 13:42:42 +03:00
/ * *
* Reset rate limit instances ( not the brute table )
* /
const resetRateLimits = async ( ) => {
// Reset rate limiting instances
const { spamPrevention } = require ( '../../core/server/web/shared/middleware/api' ) ;
spamPrevention . reset ( ) ;
} ;
2022-02-11 19:19:07 +03:00
/ * *
* This function ensures that Ghost ' s data is reset back to "factory settings"
*
* /
const resetData = async ( ) => {
// Calling reset on the database also causes the fixtures to be re-run
// We need to unhook the settings events and restore the cache before we do this
// Otherwise, the fixtures being restored will refer to the old settings cache data
settingsService . reset ( ) ;
// Clear out the database
await db . reset ( { truncate : true } ) ;
2022-10-05 13:42:42 +03:00
// Reset rate limiting instances (resetting the table is not enough!)
await resetRateLimits ( ) ;
2021-12-07 19:21:48 +03:00
} ;
2022-02-22 06:54:06 +03:00
/ * *
* Creates a ContentAPITestAgent which is a drop - in substitution for supertest .
* It is automatically hooked up to the Content API so you can make requests to e . g .
* agent . get ( '/posts/' ) without having to worry about URL paths
2022-10-18 16:52:04 +03:00
* @ returns { Promise < InstanceType < ContentAPITestAgent >> } agent
2022-02-22 06:54:06 +03:00
* /
const getContentAPIAgent = async ( ) => {
try {
const app = await startGhost ( ) ;
const originURL = configUtils . config . get ( 'url' ) ;
return new ContentAPITestAgent ( app , {
2022-03-11 14:27:43 +03:00
apiURL : '/ghost/api/content/' ,
2022-02-22 06:54:06 +03:00
originURL
} ) ;
} catch ( error ) {
error . message = ` Unable to create test agent. ${ error . message } ` ;
throw error ;
}
} ;
2021-12-07 19:21:48 +03:00
/ * *
2022-02-21 07:52:22 +03:00
* Creates a AdminAPITestAgent which is a drop - in substitution for supertest .
2022-02-08 17:39:24 +03:00
* It is automatically hooked up to the Admin API so you can make requests to e . g .
* agent . get ( '/posts/' ) without having to worry about URL paths
*
2022-02-11 15:32:08 +03:00
* @ param { Object } [ options = { } ]
2022-02-11 15:44:58 +03:00
* @ param { Boolean } [ options . members ] Include members in the boot process
2022-10-18 16:52:04 +03:00
* @ returns { Promise < InstanceType < AdminAPITestAgent >> } agent
2021-12-07 19:21:48 +03:00
* /
2022-02-11 15:32:08 +03:00
const getAdminAPIAgent = async ( options = { } ) => {
const bootOptions = { } ;
if ( options . members ) {
bootOptions . frontend = true ;
}
2022-02-06 19:18:12 +03:00
try {
2022-02-11 15:32:08 +03:00
const app = await startGhost ( bootOptions ) ;
2022-02-06 19:18:12 +03:00
const originURL = configUtils . config . get ( 'url' ) ;
2022-02-21 07:04:47 +03:00
return new AdminAPITestAgent ( app , {
2022-03-11 14:27:43 +03:00
apiURL : '/ghost/api/admin/' ,
2022-02-06 19:18:12 +03:00
originURL
} ) ;
} catch ( error ) {
2022-02-10 15:03:47 +03:00
error . message = ` Unable to create test agent. ${ error . message } ` ;
2022-02-11 13:21:45 +03:00
throw error ;
2022-02-06 19:18:12 +03:00
}
2021-12-07 12:51:08 +03:00
} ;
2022-02-11 16:11:37 +03:00
/ * *
2022-02-21 07:52:22 +03:00
* Creates a MembersAPITestAgent which is a drop - in substitution for supertest
2022-02-11 16:11:37 +03:00
* It is automatically hooked up to the Members API so you can make requests to e . g .
* agent . get ( '/webhooks/stripe/' ) without having to worry about URL paths
*
2022-10-18 16:52:04 +03:00
* @ returns { Promise < InstanceType < MembersAPITestAgent >> } agent
2022-02-11 16:11:37 +03:00
* /
const getMembersAPIAgent = async ( ) => {
const bootOptions = {
frontend : true
} ;
try {
const app = await startGhost ( bootOptions ) ;
const originURL = configUtils . config . get ( 'url' ) ;
2022-02-21 07:04:47 +03:00
return new MembersAPITestAgent ( app , {
2022-02-11 16:11:37 +03:00
apiURL : '/members/' ,
originURL
} ) ;
} catch ( error ) {
error . message = ` Unable to create test agent. ${ error . message } ` ;
throw error ;
}
} ;
2023-01-20 16:32:50 +03:00
/ * *
* Creates a MembersAPITestAgent which is a drop - in substitution for supertest
* It is automatically hooked up to the Members API so you can make requests to e . g .
* agent . get ( '/webhooks/stripe/' ) without having to worry about URL paths
*
* @ returns { Promise < InstanceType < GhostAPITestAgent >> } agent
* /
const getWebmentionsAPIAgent = async ( ) => {
const bootOptions = {
frontend : true
} ;
try {
const app = await startGhost ( bootOptions ) ;
const originURL = configUtils . config . get ( 'url' ) ;
return new GhostAPITestAgent ( app , {
apiURL : '/webmentions/' ,
originURL
} ) ;
} catch ( error ) {
error . message = ` Unable to create test agent. ${ error . message } ` ;
throw error ;
}
} ;
2022-05-20 08:17:00 +03:00
/ * *
* Creates a GhostAPITestAgent , which is a drop - in substitution for supertest
* It is automatically hooked up to the Ghost API so you can make requests to e . g .
* agent . get ( '/well-known/jwks.json' ) without having to worry about URL paths
*
2022-10-18 16:52:04 +03:00
* @ returns { Promise < InstanceType < GhostAPITestAgent >> } agent
2022-05-20 08:17:00 +03:00
* /
const getGhostAPIAgent = async ( ) => {
const bootOptions = {
frontend : false
} ;
try {
const app = await startGhost ( bootOptions ) ;
const originURL = configUtils . config . get ( 'url' ) ;
return new GhostAPITestAgent ( app , {
apiURL : '/ghost/' ,
originURL
} ) ;
} catch ( error ) {
error . message = ` Unable to create test agent. ${ error . message } ` ;
throw error ;
}
} ;
2022-02-17 11:31:48 +03:00
/ * *
*
2022-10-18 16:52:04 +03:00
* @ returns { Promise < { adminAgent : InstanceType < AdminAPITestAgent > , membersAgent : InstanceType < MembersAPITestAgent > } > } agents
2022-02-17 11:31:48 +03:00
* /
const getAgentsForMembers = async ( ) => {
let membersAgent ;
let adminAgent ;
const bootOptions = {
frontend : true
} ;
try {
const app = await startGhost ( bootOptions ) ;
const originURL = configUtils . config . get ( 'url' ) ;
2022-02-21 07:04:47 +03:00
membersAgent = new MembersAPITestAgent ( app , {
2022-02-17 11:31:48 +03:00
apiURL : '/members/' ,
originURL
} ) ;
2022-02-21 07:04:47 +03:00
adminAgent = new AdminAPITestAgent ( app , {
2022-03-11 14:27:43 +03:00
apiURL : '/ghost/api/admin/' ,
2022-02-17 11:31:48 +03:00
originURL
} ) ;
} catch ( error ) {
error . message = ` Unable to create test agent. ${ error . message } ` ;
throw error ;
}
return {
adminAgent ,
membersAgent
} ;
} ;
2022-09-27 19:44:20 +03:00
/ * *
2023-01-20 15:41:36 +03:00
* WARNING : when using this , you should stop the returned ghostServer after the tests .
2022-11-01 12:26:08 +03:00
* @ NOTE : for now method returns a supertest agent for Frontend instead of test agent with snapshot support .
2022-11-02 08:10:49 +03:00
* frontendAgent should be returning an instance of TestAgent ( related : https : //github.com/TryGhost/Toolbox/issues/471)
* @ returns { Promise < { adminAgent : InstanceType < AdminAPITestAgent > , membersAgent : InstanceType < MembersAPITestAgent > , frontendAgent : InstanceType < supertest . SuperAgentTest > , contentAPIAgent : InstanceType < ContentAPITestAgent > , ghostServer : Express . Application } > } agents
2022-09-27 19:44:20 +03:00
* /
const getAgentsWithFrontend = async ( ) => {
2022-11-02 08:10:49 +03:00
let ghostServer ;
2022-09-27 19:44:20 +03:00
let membersAgent ;
let adminAgent ;
let frontendAgent ;
2022-11-01 12:26:08 +03:00
let contentAPIAgent ;
2022-09-27 19:44:20 +03:00
const bootOptions = {
frontend : true ,
server : true
} ;
try {
Fixed configUtils and adapter cache issues in E2E tests (#16167)
no issue
There are a couple of issues with resetting the Ghost instance between
E2E test files:
These issues came to the surface because of new tests written in
https://github.com/TryGhost/Ghost/pull/16117
**1. configUtils.restore does not work correctly**
`config.reset()` is a callback based method. On top of that, it doesn't
really work reliably (https://github.com/indexzero/nconf/issues/93)
What kinda happens, is that you first call `config.reset` but
immediately after you correcty reset the config using the `config.set`
calls afterwards. But since `config.reset` is async, that reset will
happen after all those sets, and the end result is that it isn't reset
correctly.
This mainly caused issues in the new updated images tests, which were
updating the config `imageOptimization.contentImageSizes`, which is a
deeply nested config value. Maybe some references to objects are reused
in nconf that cause this issue?
Wrapping `config.reset()` in a promise does fix the issue.
**2. Adapters cache not reset between tests**
At the start of each test, we set `paths:contentPath` to a nice new
temporary directory. But if a previous test already requests a
localStorage adapter, that adapter would have been created and in the
constructor `paths:contentPath` would have been passed. That same
instance will be reused in the next test run. So it won't read the new
config again. To fix this, we need to reset the adapter instances
between E2E tests.
How was this visible? Test uploads were stored in the actual git
repository, and not in a temporary directory. When writing the new image
upload tests, this also resulted in unreliable test runs because some
image names were already taken (from previous test runs).
**3. Old 2E2 test Ghost server not stopped**
Sometimes we still need access to the frontend test server using
`getAgentsWithFrontend`. But that does start a new Ghost server which is
actually listening for HTTP traffic. This could result in a fatal error
in tests because the port is already in use. The issue is that old E2E
tests also start a HTTP server, but they don't stop the server. When you
used the old `startGhost` util, it would check if a server was already
running and stop it first. The new `getAgentsWithFrontend` now also has
the same functionality to fix that issue.
2023-01-30 16:06:20 +03:00
// Possible that we still have a running Ghost server from a previous old E2E test
// Those tests never stopped the server in the tests manually
await stopGhost ( ) ;
// Start a new Ghost server with real HTTP listener
2022-11-02 08:10:49 +03:00
ghostServer = await startGhost ( bootOptions ) ;
const app = ghostServer . rootApp ;
2022-09-27 19:44:20 +03:00
const originURL = configUtils . config . get ( 'url' ) ;
membersAgent = new MembersAPITestAgent ( app , {
apiURL : '/members/' ,
originURL
} ) ;
adminAgent = new AdminAPITestAgent ( app , {
apiURL : '/ghost/api/admin/' ,
originURL
} ) ;
2022-11-01 12:26:08 +03:00
contentAPIAgent = new ContentAPITestAgent ( app , {
apiURL : '/ghost/api/content/' ,
originURL
} ) ;
2022-09-27 19:44:20 +03:00
frontendAgent = supertest . agent ( originURL ) ;
} catch ( error ) {
error . message = ` Unable to create test agent. ${ error . message } ` ;
throw error ;
}
return {
adminAgent ,
membersAgent ,
2022-11-01 12:26:08 +03:00
frontendAgent ,
2022-11-02 08:10:49 +03:00
contentAPIAgent ,
// @NOTE: ghost server should not be exposed ideally, it's a hack (see commit message)
ghostServer
2022-09-27 19:44:20 +03:00
} ;
} ;
2022-05-30 10:05:17 +03:00
const insertWebhook = ( { event , url } ) => {
return fixtureUtils . fixtures . insertWebhook ( {
event : event ,
target _url : url
} ) ;
} ;
2022-12-02 11:44:32 +03:00
class Nullable extends AsymmetricMatcher {
constructor ( sample ) {
super ( sample ) ;
}
asymmetricMatch ( other ) {
if ( other === null ) {
return true ;
}
return this . sample . asymmetricMatch ( other ) ;
}
toString ( ) {
return ` Nullable< ${ this . sample . toString ( ) } > ` ;
}
getExpectedType ( ) {
return ` null| ${ this . sample . getExpectedType ( ) } ` ;
}
toAsymmetricMatcher ( ) {
return ` Nullable< ${ this . sample . toAsymmetricMatcher ? this . sample . toAsymmetricMatcher ( ) : this . sample . toString ( ) } > ` ;
}
}
2022-02-07 19:02:04 +03:00
module . exports = {
// request agent
agentProvider : {
2022-02-11 16:11:37 +03:00
getAdminAPIAgent ,
2022-02-17 11:31:48 +03:00
getMembersAPIAgent ,
2023-01-20 16:32:50 +03:00
getWebmentionsAPIAgent ,
2022-02-22 06:54:06 +03:00
getContentAPIAgent ,
2022-05-20 08:17:00 +03:00
getAgentsForMembers ,
2022-09-27 19:44:20 +03:00
getGhostAPIAgent ,
getAgentsWithFrontend
2022-02-07 19:02:04 +03:00
} ,
2022-12-06 13:30:07 +03:00
// @NOTE: startGhost only exposed for playwright tests
startGhost ,
2022-02-07 19:02:04 +03:00
// Mocks and Stubs
2022-02-08 19:52:38 +03:00
mockManager ,
2022-02-07 19:02:04 +03:00
// DB State Manipulation
fixtureManager : {
get : getFixture ,
2022-05-30 10:05:17 +03:00
insertWebhook : insertWebhook ,
2022-02-08 20:13:30 +03:00
getCurrentOwnerUser : fixtureUtils . getCurrentOwnerUser ,
2022-02-07 19:02:04 +03:00
init : initFixtures ,
2022-03-21 14:07:21 +03:00
restore : resetData ,
getPathForFixture : ( fixturePath ) => {
return path . join ( _ _dirname , 'fixtures' , fixturePath ) ;
}
2022-02-08 17:33:19 +03:00
} ,
2023-04-06 23:42:11 +03:00
regexes : {
2023-04-07 15:34:38 +03:00
anyMajorMinorVersion : /v\d+\.\d+/gi ,
queryStringToken : paramName => new RegExp ( ` ${ paramName } =( \\ w|-)+ ` , 'g' )
2023-04-06 23:42:11 +03:00
} ,
2022-02-08 19:36:08 +03:00
matchers : {
2022-07-06 17:44:20 +03:00
anyBoolean : any ( Boolean ) ,
2022-02-08 19:36:08 +03:00
anyString : any ( String ) ,
2022-02-07 15:51:21 +03:00
anyArray : any ( Array ) ,
2022-07-25 18:48:23 +03:00
anyObject : any ( Object ) ,
2022-05-06 14:15:12 +03:00
anyNumber : any ( Number ) ,
2022-12-02 11:44:32 +03:00
nullable : expectedObject => new Nullable ( expectedObject ) , // usage: nullable(anyString)
2022-05-19 08:06:50 +03:00
anyStringNumber : stringMatching ( /\d+/ ) ,
2022-03-01 14:17:13 +03:00
anyISODateTime : stringMatching ( /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.000Z/ ) ,
anyISODate : stringMatching ( /\d{4}-\d{2}-\d{2}/ ) ,
anyISODateTimeWithTZ : stringMatching ( /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.000\+\d{2}:\d{2}/ ) ,
2022-02-08 19:36:08 +03:00
anyEtag : stringMatching ( /(?:W\/)?"(?:[ !#-\x7E\x80-\xFF]*|\r\n[\t ]|\\.)*"/ ) ,
2022-05-19 08:19:05 +03:00
anyContentLength : stringMatching ( /\d+/ ) ,
anyContentVersion : stringMatching ( /v\d+\.\d+/ ) ,
2022-02-08 19:36:08 +03:00
anyObjectId : stringMatching ( /[a-f0-9]{24}/ ) ,
anyErrorId : stringMatching ( /[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}/ ) ,
anyUuid : stringMatching ( /[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}/ ) ,
2022-02-16 15:50:58 +03:00
anyLocationFor : ( resource ) => {
return stringMatching ( new RegExp ( ` https?://.*?/ ${ resource } /[a-f0-9]{24}/ ` ) ) ;
} ,
2022-10-06 03:56:10 +03:00
anyGhostAgent : stringMatching ( /Ghost\/\d+\.\d+\.\d+\s\(https:\/\/github.com\/TryGhost\/Ghost\)/ ) ,
2022-10-05 12:22:08 +03:00
// @NOTE: hack here! it's due to https://github.com/TryGhost/Toolbox/issues/341
// this matcher should be removed once the issue is solved - routing is redesigned
// An ideal solution would be removal of this matcher altogether.
2022-10-24 16:55:25 +03:00
anyLocalURL : stringMatching ( /http:\/\/127.0.0.1:2369\/[A-Za-z0-9_-]+\// ) ,
2022-02-08 19:36:08 +03:00
stringMatching
2022-05-06 14:15:12 +03:00
} ,
// utilities
configUtils : require ( './configUtils' ) ,
dbUtils : require ( './db-utils' ) ,
2022-10-05 13:42:42 +03:00
urlUtils : require ( './urlUtils' ) ,
resetRateLimits
2022-02-07 19:02:04 +03:00
} ;