2017-03-09 22:38:20 +03:00
/*jshint expr:true*/
var Promise = require ( 'bluebird' ) ,
_ = require ( 'lodash' ) ,
fs = require ( 'fs-extra' ) ,
path = require ( 'path' ) ,
Module = require ( 'module' ) ,
2017-09-21 17:05:35 +03:00
os = require ( 'os' ) ,
2017-08-15 14:29:27 +03:00
debug = require ( 'ghost-ignition' ) . debug ( 'test' ) ,
2017-03-09 22:38:20 +03:00
ObjectId = require ( 'bson-objectid' ) ,
uuid = require ( 'uuid' ) ,
KnexMigrator = require ( 'knex-migrator' ) ,
ghost = require ( '../../server' ) ,
errors = require ( '../../server/errors' ) ,
db = require ( '../../server/data/db' ) ,
fixtureUtils = require ( '../../server/data/schema/fixtures/utils' ) ,
schema = require ( '../../server/data/schema' ) . tables ,
schemaTables = Object . keys ( schema ) ,
models = require ( '../../server/models' ) ,
SettingsLib = require ( '../../server/settings' ) ,
permissions = require ( '../../server/permissions' ) ,
sequence = require ( '../../server/utils/sequence' ) ,
themes = require ( '../../server/themes' ) ,
2013-11-07 17:26:47 +04:00
DataGenerator = require ( './fixtures/data-generator' ) ,
2017-09-21 17:05:35 +03:00
configUtils = require ( './configUtils' ) ,
2017-03-09 22:38:20 +03:00
filterData = require ( './fixtures/filter-param' ) ,
API = require ( './api' ) ,
fork = require ( './fork' ) ,
mocks = require ( './mocks' ) ,
config = require ( '../../server/config' ) ,
knexMigrator = new KnexMigrator ( ) ,
2014-07-21 21:50:04 +04:00
fixtures ,
getFixtureOps ,
toDoList ,
2016-05-19 14:49:22 +03:00
originalRequireFn ,
2014-07-21 21:50:04 +04:00
postsInserted = 0 ,
2013-05-24 08:02:41 +04:00
2016-05-19 14:49:22 +03:00
mockNotExistingModule ,
unmockNotExistingModule ,
2014-07-21 21:50:04 +04:00
teardown ,
setup ,
doAuth ,
✨ replace auto increment id's by object id (#7495)
* 🛠 bookshelf tarball, bson-objectid
* 🎨 schema changes
- change increment type to string
- add a default fallback for string length 191 (to avoid adding this logic to every single column which uses an ID)
- remove uuid, because ID now represents a global resource identifier
- keep uuid for post, because we are using this as preview id
- keep uuid for clients for now - we are using this param for Ghost-Auth
* ✨ base model: generate ObjectId on creating event
- each new resource get's a auto generate ObjectId
- this logic won't work for attached models, this commit comes later
* 🎨 centralised attach method
When attaching models there are two things important two know
1. To be able to attach an ObjectId, we need to register the `onCreating` event the fetched model!This is caused by the Bookshelf design in general. On this target model we are attaching the new model.
2. We need to manually fetch the target model, because Bookshelf has a weird behaviour (which is known as a bug, see see https://github.com/tgriesser/bookshelf/issues/629). The most important property when attaching a model is `parentFk`, which is the foreign key. This can be null when fetching the model with the option `withRelated`. To ensure quality and consistency, the custom attach wrapper always fetches the target model manual. By fetching the target model (again) is a little performance decrease, but it also has advantages: we can register the event, and directly unregister the event again. So very clean code.
Important: please only use the custom attach wrapper in the future.
* 🎨 token model had overriden the onCreating function because of the created_at field
- we need to ensure that the base onCreating hook get's triggered for ALL models
- if not, they don't get an ObjectId assigned
- in this case: be smart and check if the target model has a created_at field
* 🎨 we don't have a uuid field anymore, remove the usages
- no default uuid creation in models
- i am pretty sure we have some more definitions in our tests (for example in the export json files), but that is too much work to delete them all
* 🎨 do not parse ID to Number
- we had various occurances of parsing all ID's to numbers
- we don't need this behaviour anymore
- ID is string
- i will adapt the ID validation in the next commit
* 🎨 change ID regex for validation
- we only allow: ID as ObjectId, ID as 1 and ID as me
- we need to keep ID 1, because our whole software relies on ID 1 (permissions etc)
* 🎨 owner fixture
- roles: [4] does not work anymore
- 4 means -> static id 4
- this worked in an auto increment system (not even in a system with distributed writes)
- with ObjectId we generate each ID automatically (for static and dynamic resources)
- it is possible to define all id's for static resources still, but that means we need to know which ID is already used and for consistency we have to define ObjectId's for these static resources
- so no static id's anymore, except of: id 1 for owner and id 0 for external usage (because this is required from our permission system)
- NOTE: please read through the comment in the user model
* 🎨 tests: DataGenerator and test utils
First of all: we need to ensure using ObjectId's in the tests. When don't, we can't ensure that ObjectId's work properly.
This commit brings lot's of dynamic into all the static defined id's.
In one of the next commits, i will adapt all the tests.
* 🚨 remove counter in Notification API
- no need to add a counter
- we simply generate ObjectId's (they are auto incremental as well)
- our id validator does only allow ObjectId as id,1 and me
* 🎨 extend contextUser in Base Model
- remove isNumber check, because id's are no longer numbers, except of id 0/1
- use existing isExternalUser
- support id 0/1 as string or number
* ✨ Ghost Owner has id 1
- ensure we define this id in the fixtures.json
- doesn't matter if number or string
* 🎨 functional tests adaptions
- use dynamic id's
* 🎨 fix unit tests
* 🎨 integration tests adaptions
* 🎨 change importer utils
- all our export examples (test/fixtures/exports) contain id's as numbers
- fact: but we ignore them anyway when inserting into the database, see https://github.com/TryGhost/Ghost/blob/master/core/server/data/import/utils.js#L249
- in https://github.com/TryGhost/Ghost/pull/7495/commits/0e6ed957cd54dc02a25cf6fb1ab7d7e723295e2c#diff-70f514a06347c048648be464819503c4L67 i removed parsing id's to integers
- i realised that this ^ check just existed, because the userIdToMap was an object key and object keys are always strings!
- i think this logic is a little bit complicated, but i don't want to refactor this now
- this commit ensures when trying to find the user, the id comparison works again
- i've added more documentation to understand this logic ;)
- plus i renamed an attribute to improve readability
* 🎨 Data-Generator: add more defaults to createUser
- if i use the function DataGenerator.forKnex.createUser i would like to get a full set of defaults
* 🎨 test utils: change/extend function set for functional tests
- functional tests work a bit different
- they boot Ghost and seed the database
- some functional tests have mis-used the test setup
- the test setup needs two sections: integration/unit and functional tests
- any functional test is allowed to either add more data or change data in the existing Ghost db
- but what it should not do is: add test fixtures like roles or users from our DataGenerator and cross fingers it will work
- this commit adds a clean method for functional tests to add extra users
* 🎨 functional tests adaptions
- use last commit to insert users for functional tests clean
- tidy up usage of testUtils.setup or testUtils.doAuth
* 🐛 test utils: reset database before init
- ensure we don't have any left data from other tests in the database when starting ghost
* 🐛 fix test (unrelated to this PR)
- fixes a random failure
- return statement was missing
* 🎨 make changes for invites
2016-11-17 12:09:11 +03:00
createUser ,
2017-09-26 17:50:26 +03:00
createPost ,
2014-10-19 23:10:13 +04:00
login ,
togglePermalinks ,
2016-10-11 15:37:11 +03:00
startGhost ,
2013-06-15 02:12:04 +04:00
2014-08-09 23:16:54 +04:00
initFixtures ,
2014-07-21 21:50:04 +04:00
initData ,
2016-11-08 14:33:19 +03:00
clearData ,
clearBruteData ;
2013-06-15 02:12:04 +04:00
🎉 🎨 ✨ Remove middleware/index.js (#7548)
closes #4172, closes #6948, refs #7491, refs #7488, refs #7542, refs #7484
* 🎨 Co-locate all admin-related code in /admin
- move all the admin related code from controllers, routes and helpers into a single location
- add error handling middleware explicitly to adminApp
- re-order blogApp middleware to ensure the shared middleware is mounted after the adminApp
- TODO: rethink the structure of /admin, this should probably be an internal app
* 💄 Group global middleware together
- There are only a few pieces of middleware which are "global"
- These are needed for the admin, blog and api
- Everything else is only needed in one or two places
* ✨ Introduce a separate blogApp
- create a brand-new blogApp
- mount all blog/theme only middleware etc onto blogApp
- mount error handling on blogApp only
* 🎨 Separate error handling for HTML & API JSON
- split JSON and HTML error handling into separate functions
- re-introduce a way to not output the stack for certain errors
- add more tests around errors & an assertion framework for checking JSON Errors
- TODO: better 404 handling for static assets
Rationale:
The API is very different to the blog/admin panel:
- It is intended to only ever serve JSON, never HTML responses
- It is intended to always serve JSON
Meanwhile the blog and admin panel have no need for JSON errors,
when an error happens on those pages, we should serve HTML pages
which are nicely formatted with the error & using the correct template
* 🐛 Fix checkSSL to work for subapps
- in order to make this work on a sub app we need to use the pattern `req.originalUrl || req.url`
* 🔥 Get rid of decide-is-admin (part 1/2)
- delete decide-is-admin & tests
- add two small functions to apiApp and adminApp to set res.isAdmin
- mount checkSSL on all the apps
- TODO: deduplicate the calls to checkSSL by making blogApp a subApp :D
- PART 2/2: finish cleaning this up by removing it from where it's not needed and giving it a more specific name
Rationale:
Now that we have both an adminApp and an apiApp,
we can temporarily replace this weird path-matching middleware
with middleware that sets res.isAdmin for api & admin
* 🎨 Wire up prettyURLs on all Apps
- prettyURLs is needed for all requests
- it cannot be global because it has to live after asset middleware, and before routing
- this does not result in duplicate redirects, but does result in duplicate checks
- TODO: resolve extra middleware in stack by making blogApp a sub app
* ⏱ Add debug to API setup
* 🎨 Rename blogApp -> parentApp in middleware
* 🎨 Co-locate all blog-related code in /blog
- Move all of the blogApp code from middleware/index.js to blog/app.js
- Move routes/frontend.js to blog/routes.js
- Remove the routes/index.js and routes folder, this is empty now!
- @TODO is blog the best name for this? 🤔
- @TODO sort out the big hunk of asset-related mess
- @TODO also separate out the concept of theme from blog
* 🎉 Replace middleware index with server/app.js
- The final piece of the puzzle! 🎉 🎈 🎂
- We no longer have our horrendous middleware/index.js
- Instead, we have a set of app.js files, which all use a familiar pattern
* 💄 Error handling fixups
2016-10-13 18:24:09 +03:00
// Require additional assertions which help us keep our tests small and clear
require ( './assertions' ) ;
2014-07-21 21:50:04 +04:00
/** TEST FIXTURES **/
fixtures = {
2016-06-10 08:12:46 +03:00
insertPosts : function insertPosts ( posts ) {
return Promise . resolve ( db . knex ( 'posts' ) . insert ( posts ) ) ;
} ,
2016-05-19 14:49:22 +03:00
insertPostsAndTags : function insertPostsAndTags ( ) {
2016-02-12 14:56:27 +03:00
return Promise . resolve ( db . knex ( 'posts' ) . insert ( DataGenerator . forKnex . posts ) ) . then ( function ( ) {
return db . knex ( 'tags' ) . insert ( DataGenerator . forKnex . tags ) ;
2014-07-23 12:32:27 +04:00
} ) . then ( function ( ) {
2016-02-12 14:56:27 +03:00
return db . knex ( 'posts_tags' ) . insert ( DataGenerator . forKnex . posts _tags ) ;
2014-07-23 12:32:27 +04:00
} ) ;
} ,
insertMultiAuthorPosts : function insertMultiAuthorPosts ( max ) {
2014-09-03 19:42:55 +04:00
/*jshint unused:false*/
2016-02-12 14:56:27 +03:00
var author ,
2014-07-23 12:32:27 +04:00
authors ,
i , j , k = postsInserted ,
posts = [ ] ;
max = max || 50 ;
// insert users of different roles
2014-09-03 19:42:55 +04:00
return Promise . resolve ( fixtures . createUsersWithRoles ( ) ) . then ( function ( ) {
2014-07-23 12:32:27 +04:00
// create the tags
2016-02-12 14:56:27 +03:00
return db . knex ( 'tags' ) . insert ( DataGenerator . forKnex . tags ) ;
2014-09-03 19:42:55 +04:00
} ) . then ( function ( ) {
2016-02-12 14:56:27 +03:00
return db . knex ( 'users' ) . select ( 'id' ) ;
2014-07-23 12:32:27 +04:00
} ) . then ( function ( results ) {
2016-06-11 21:23:27 +03:00
authors = _ . map ( results , 'id' ) ;
2014-07-23 12:32:27 +04:00
// Let's insert posts with random authors
for ( i = 0 ; i < max ; i += 1 ) {
author = authors [ i % authors . length ] ;
2014-09-03 19:42:55 +04:00
posts . push ( DataGenerator . forKnex . createGenericPost ( k , null , null , author ) ) ;
k = k + 1 ;
2014-07-23 12:32:27 +04:00
}
// Keep track so we can run this function again safely
postsInserted = k ;
return sequence ( _ . times ( posts . length , function ( index ) {
return function ( ) {
2016-02-12 14:56:27 +03:00
return db . knex ( 'posts' ) . insert ( posts [ index ] ) ;
2014-07-23 12:32:27 +04:00
} ;
} ) ) ;
} ) . then ( function ( ) {
2014-08-17 10:17:23 +04:00
return Promise . all ( [
2016-02-12 14:56:27 +03:00
db . knex ( 'posts' ) . orderBy ( 'id' , 'asc' ) . select ( 'id' ) ,
db . knex ( 'tags' ) . select ( 'id' )
2014-07-23 12:32:27 +04:00
] ) ;
} ) . then ( function ( results ) {
2016-06-11 21:23:27 +03:00
var posts = _ . map ( results [ 0 ] , 'id' ) ,
tags = _ . map ( results [ 1 ] , 'id' ) ,
2014-07-23 12:32:27 +04:00
promises = [ ] ,
i ;
if ( max > posts . length ) {
2014-08-17 10:17:23 +04:00
throw new Error ( 'Trying to add more posts_tags than the number of posts. ' + max + ' ' + posts . length ) ;
2014-07-23 12:32:27 +04:00
}
for ( i = 0 ; i < max ; i += 1 ) {
promises . push ( DataGenerator . forKnex . createPostsTags ( posts [ i ] , tags [ i % tags . length ] ) ) ;
}
return sequence ( _ . times ( promises . length , function ( index ) {
return function ( ) {
2016-02-12 14:56:27 +03:00
return db . knex ( 'posts_tags' ) . insert ( promises [ index ] ) ;
2014-07-23 12:32:27 +04:00
} ;
} ) ) ;
} ) ;
2014-07-21 21:50:04 +04:00
} ,
2013-12-31 21:09:49 +04:00
2014-07-21 21:50:04 +04:00
insertMorePosts : function insertMorePosts ( max ) {
var lang ,
status ,
posts = [ ] ,
2016-02-12 14:56:27 +03:00
i , j , k = postsInserted ;
2013-06-15 02:12:04 +04:00
2014-07-21 21:50:04 +04:00
max = max || 50 ;
2013-08-25 00:51:58 +04:00
2014-07-21 21:50:04 +04:00
for ( i = 0 ; i < 2 ; i += 1 ) {
lang = i % 2 ? 'en' : 'fr' ;
2014-09-03 19:42:55 +04:00
posts . push ( DataGenerator . forKnex . createGenericPost ( k , null , lang ) ) ;
k = k + 1 ;
2014-02-13 07:26:56 +04:00
2014-07-21 21:50:04 +04:00
for ( j = 0 ; j < max ; j += 1 ) {
status = j % 2 ? 'draft' : 'published' ;
2014-09-03 19:42:55 +04:00
posts . push ( DataGenerator . forKnex . createGenericPost ( k , status , lang ) ) ;
k = k + 1 ;
2014-07-21 21:50:04 +04:00
}
2014-02-13 07:26:56 +04:00
}
2014-07-21 21:50:04 +04:00
// Keep track so we can run this function again safely
postsInserted = k ;
return sequence ( _ . times ( posts . length , function ( index ) {
2014-07-17 18:33:21 +04:00
return function ( ) {
2016-02-12 14:56:27 +03:00
return db . knex ( 'posts' ) . insert ( posts [ index ] ) ;
2014-03-16 20:21:02 +04:00
} ;
} ) ) ;
2014-07-21 21:50:04 +04:00
} ,
2015-07-29 21:07:58 +03:00
insertMoreTags : function insertMoreTags ( max ) {
max = max || 50 ;
var tags = [ ] ,
tagName ,
2016-02-12 14:56:27 +03:00
i ;
2015-07-29 21:07:58 +03:00
for ( i = 0 ; i < max ; i += 1 ) {
tagName = uuid . v4 ( ) . split ( '-' ) [ 0 ] ;
tags . push ( DataGenerator . forKnex . createBasic ( { name : tagName , slug : tagName } ) ) ;
}
return sequence ( _ . times ( tags . length , function ( index ) {
return function ( ) {
2016-02-12 14:56:27 +03:00
return db . knex ( 'tags' ) . insert ( tags [ index ] ) ;
2015-07-29 21:07:58 +03:00
} ;
} ) ) ;
} ,
2014-07-21 21:50:04 +04:00
insertMorePostsTags : function insertMorePostsTags ( max ) {
max = max || 50 ;
2014-08-17 10:17:23 +04:00
return Promise . all ( [
2016-02-12 14:56:27 +03:00
db . knex ( 'posts' ) . orderBy ( 'id' , 'asc' ) . select ( 'id' ) ,
db . knex ( 'tags' ) . select ( 'id' , 'name' )
2014-07-21 21:50:04 +04:00
] ) . then ( function ( results ) {
2016-06-11 21:23:27 +03:00
var posts = _ . map ( results [ 0 ] , 'id' ) ,
2014-07-21 21:50:04 +04:00
injectionTagId = _ . chain ( results [ 1 ] )
2016-06-11 21:23:27 +03:00
. filter ( { name : 'injection' } )
. map ( 'id' )
2014-07-21 21:50:04 +04:00
. value ( ) [ 0 ] ,
promises = [ ] ,
i ;
if ( max > posts . length ) {
throw new Error ( 'Trying to add more posts_tags than the number of posts.' ) ;
}
for ( i = 0 ; i < max ; i += 1 ) {
promises . push ( DataGenerator . forKnex . createPostsTags ( posts [ i ] , injectionTagId ) ) ;
2014-07-16 01:43:22 +04:00
}
2014-07-21 21:50:04 +04:00
return sequence ( _ . times ( promises . length , function ( index ) {
return function ( ) {
2016-02-12 14:56:27 +03:00
return db . knex ( 'posts_tags' ) . insert ( promises [ index ] ) ;
2014-07-21 21:50:04 +04:00
} ;
} ) ) ;
2014-02-12 07:40:39 +04:00
} ) ;
2014-07-21 21:50:04 +04:00
} ,
2016-09-21 17:48:14 +03:00
2014-07-30 19:40:30 +04:00
insertRoles : function insertRoles ( ) {
2016-02-12 14:56:27 +03:00
return db . knex ( 'roles' ) . insert ( DataGenerator . forKnex . roles ) ;
2014-07-30 19:40:30 +04:00
} ,
2013-12-29 00:13:47 +04:00
2014-07-21 21:50:04 +04:00
initOwnerUser : function initOwnerUser ( ) {
2016-02-12 14:56:27 +03:00
var user = DataGenerator . Content . users [ 0 ] ;
2014-07-21 21:50:04 +04:00
user = DataGenerator . forKnex . createBasic ( user ) ;
2014-09-10 08:06:24 +04:00
user = _ . extend ( { } , user , { status : 'inactive' } ) ;
2014-07-21 21:50:04 +04:00
✨ replace auto increment id's by object id (#7495)
* 🛠 bookshelf tarball, bson-objectid
* 🎨 schema changes
- change increment type to string
- add a default fallback for string length 191 (to avoid adding this logic to every single column which uses an ID)
- remove uuid, because ID now represents a global resource identifier
- keep uuid for post, because we are using this as preview id
- keep uuid for clients for now - we are using this param for Ghost-Auth
* ✨ base model: generate ObjectId on creating event
- each new resource get's a auto generate ObjectId
- this logic won't work for attached models, this commit comes later
* 🎨 centralised attach method
When attaching models there are two things important two know
1. To be able to attach an ObjectId, we need to register the `onCreating` event the fetched model!This is caused by the Bookshelf design in general. On this target model we are attaching the new model.
2. We need to manually fetch the target model, because Bookshelf has a weird behaviour (which is known as a bug, see see https://github.com/tgriesser/bookshelf/issues/629). The most important property when attaching a model is `parentFk`, which is the foreign key. This can be null when fetching the model with the option `withRelated`. To ensure quality and consistency, the custom attach wrapper always fetches the target model manual. By fetching the target model (again) is a little performance decrease, but it also has advantages: we can register the event, and directly unregister the event again. So very clean code.
Important: please only use the custom attach wrapper in the future.
* 🎨 token model had overriden the onCreating function because of the created_at field
- we need to ensure that the base onCreating hook get's triggered for ALL models
- if not, they don't get an ObjectId assigned
- in this case: be smart and check if the target model has a created_at field
* 🎨 we don't have a uuid field anymore, remove the usages
- no default uuid creation in models
- i am pretty sure we have some more definitions in our tests (for example in the export json files), but that is too much work to delete them all
* 🎨 do not parse ID to Number
- we had various occurances of parsing all ID's to numbers
- we don't need this behaviour anymore
- ID is string
- i will adapt the ID validation in the next commit
* 🎨 change ID regex for validation
- we only allow: ID as ObjectId, ID as 1 and ID as me
- we need to keep ID 1, because our whole software relies on ID 1 (permissions etc)
* 🎨 owner fixture
- roles: [4] does not work anymore
- 4 means -> static id 4
- this worked in an auto increment system (not even in a system with distributed writes)
- with ObjectId we generate each ID automatically (for static and dynamic resources)
- it is possible to define all id's for static resources still, but that means we need to know which ID is already used and for consistency we have to define ObjectId's for these static resources
- so no static id's anymore, except of: id 1 for owner and id 0 for external usage (because this is required from our permission system)
- NOTE: please read through the comment in the user model
* 🎨 tests: DataGenerator and test utils
First of all: we need to ensure using ObjectId's in the tests. When don't, we can't ensure that ObjectId's work properly.
This commit brings lot's of dynamic into all the static defined id's.
In one of the next commits, i will adapt all the tests.
* 🚨 remove counter in Notification API
- no need to add a counter
- we simply generate ObjectId's (they are auto incremental as well)
- our id validator does only allow ObjectId as id,1 and me
* 🎨 extend contextUser in Base Model
- remove isNumber check, because id's are no longer numbers, except of id 0/1
- use existing isExternalUser
- support id 0/1 as string or number
* ✨ Ghost Owner has id 1
- ensure we define this id in the fixtures.json
- doesn't matter if number or string
* 🎨 functional tests adaptions
- use dynamic id's
* 🎨 fix unit tests
* 🎨 integration tests adaptions
* 🎨 change importer utils
- all our export examples (test/fixtures/exports) contain id's as numbers
- fact: but we ignore them anyway when inserting into the database, see https://github.com/TryGhost/Ghost/blob/master/core/server/data/import/utils.js#L249
- in https://github.com/TryGhost/Ghost/pull/7495/commits/0e6ed957cd54dc02a25cf6fb1ab7d7e723295e2c#diff-70f514a06347c048648be464819503c4L67 i removed parsing id's to integers
- i realised that this ^ check just existed, because the userIdToMap was an object key and object keys are always strings!
- i think this logic is a little bit complicated, but i don't want to refactor this now
- this commit ensures when trying to find the user, the id comparison works again
- i've added more documentation to understand this logic ;)
- plus i renamed an attribute to improve readability
* 🎨 Data-Generator: add more defaults to createUser
- if i use the function DataGenerator.forKnex.createUser i would like to get a full set of defaults
* 🎨 test utils: change/extend function set for functional tests
- functional tests work a bit different
- they boot Ghost and seed the database
- some functional tests have mis-used the test setup
- the test setup needs two sections: integration/unit and functional tests
- any functional test is allowed to either add more data or change data in the existing Ghost db
- but what it should not do is: add test fixtures like roles or users from our DataGenerator and cross fingers it will work
- this commit adds a clean method for functional tests to add extra users
* 🎨 functional tests adaptions
- use last commit to insert users for functional tests clean
- tidy up usage of testUtils.setup or testUtils.doAuth
* 🐛 test utils: reset database before init
- ensure we don't have any left data from other tests in the database when starting ghost
* 🐛 fix test (unrelated to this PR)
- fixes a random failure
- return statement was missing
* 🎨 make changes for invites
2016-11-17 12:09:11 +03:00
return db . knex ( 'roles' ) . insert ( DataGenerator . forKnex . roles ) . then ( function ( ) {
return db . knex ( 'users' ) . insert ( user ) ;
} ) . then ( function ( ) {
return db . knex ( 'roles_users' ) . insert ( DataGenerator . forKnex . roles _users [ 0 ] ) ;
} ) ;
2014-07-21 21:50:04 +04:00
} ,
insertOwnerUser : function insertOwnerUser ( ) {
2016-02-12 14:56:27 +03:00
var user ;
2014-07-21 21:50:04 +04:00
user = DataGenerator . forKnex . createUser ( DataGenerator . Content . users [ 0 ] ) ;
2014-02-25 00:28:18 +04:00
2016-02-12 14:56:27 +03:00
return db . knex ( 'users' ) . insert ( user ) . then ( function ( ) {
return db . knex ( 'roles_users' ) . insert ( DataGenerator . forKnex . roles _users [ 0 ] ) ;
2014-08-09 23:16:54 +04:00
} ) ;
2014-07-21 21:50:04 +04:00
} ,
2014-02-25 00:28:18 +04:00
2015-10-24 22:04:02 +03:00
overrideOwnerUser : function overrideOwnerUser ( slug ) {
2016-02-12 14:56:27 +03:00
var user ;
2014-07-21 21:50:04 +04:00
user = DataGenerator . forKnex . createUser ( DataGenerator . Content . users [ 0 ] ) ;
2016-09-13 19:23:02 +03:00
2015-10-24 22:04:02 +03:00
if ( slug ) {
user . slug = slug ;
}
2014-02-25 00:28:18 +04:00
2016-02-12 14:56:27 +03:00
return db . knex ( 'users' )
2017-07-31 12:37:37 +03:00
. where ( 'id' , '=' , DataGenerator . Content . users [ 0 ] . id )
2014-07-21 21:50:04 +04:00
. update ( user ) ;
} ,
🐛 be able to serve locked users (#8711)
closes #8645, closes #8710
- locked users were once part of the category "active users", but were moved to the inactive category
-> we have added a protection of not being able to edit yourself when you are either suspended or locked
- but they are not really active users, they are restricted, because they have no access to the admin panel
- support three categories: active, inactive, restricted
* - revert restricted states
- instead, update permission layer: fallback to `all` by default, because you are able to serve any user status
- add more tests
- ATTENTION: there is a behaviour change, that a blog owner's author page can be served before setting up the blog, see conversation on slack
-> LTS serves 404
-> 1.0 would serve 200
2017-07-20 14:45:13 +03:00
changeOwnerUserStatus : function changeOwnerUserStatus ( options ) {
return db . knex ( 'users' )
. where ( 'slug' , '=' , options . slug )
. update ( {
status : options . status
} ) ;
} ,
2014-07-21 21:50:04 +04:00
createUsersWithRoles : function createUsersWithRoles ( ) {
2016-02-12 14:56:27 +03:00
return db . knex ( 'roles' ) . insert ( DataGenerator . forKnex . roles ) . then ( function ( ) {
return db . knex ( 'users' ) . insert ( DataGenerator . forKnex . users ) ;
2014-07-21 21:50:04 +04:00
} ) . then ( function ( ) {
2016-02-12 14:56:27 +03:00
return db . knex ( 'roles_users' ) . insert ( DataGenerator . forKnex . roles _users ) ;
2014-07-21 21:50:04 +04:00
} ) ;
} ,
2016-11-09 18:02:49 +03:00
createUsersWithoutOwner : function createUsersWithoutOwner ( ) {
2016-08-19 14:02:07 +03:00
var usersWithoutOwner = DataGenerator . forKnex . users . slice ( 1 ) ;
2016-11-09 18:02:49 +03:00
return db . knex ( 'users' ) . insert ( usersWithoutOwner )
. then ( function ( ) {
return db . knex ( 'roles_users' ) . insert ( DataGenerator . forKnex . roles _users ) ;
} ) ;
2016-08-19 14:02:07 +03:00
} ,
2014-07-24 13:46:05 +04:00
createExtraUsers : function createExtraUsers ( ) {
2016-02-12 14:56:27 +03:00
// grab 3 more users
var extraUsers = DataGenerator . Content . users . slice ( 2 , 5 ) ;
2014-07-24 13:46:05 +04:00
extraUsers = _ . map ( extraUsers , function ( user ) {
return DataGenerator . forKnex . createUser ( _ . extend ( { } , user , {
✨ replace auto increment id's by object id (#7495)
* 🛠 bookshelf tarball, bson-objectid
* 🎨 schema changes
- change increment type to string
- add a default fallback for string length 191 (to avoid adding this logic to every single column which uses an ID)
- remove uuid, because ID now represents a global resource identifier
- keep uuid for post, because we are using this as preview id
- keep uuid for clients for now - we are using this param for Ghost-Auth
* ✨ base model: generate ObjectId on creating event
- each new resource get's a auto generate ObjectId
- this logic won't work for attached models, this commit comes later
* 🎨 centralised attach method
When attaching models there are two things important two know
1. To be able to attach an ObjectId, we need to register the `onCreating` event the fetched model!This is caused by the Bookshelf design in general. On this target model we are attaching the new model.
2. We need to manually fetch the target model, because Bookshelf has a weird behaviour (which is known as a bug, see see https://github.com/tgriesser/bookshelf/issues/629). The most important property when attaching a model is `parentFk`, which is the foreign key. This can be null when fetching the model with the option `withRelated`. To ensure quality and consistency, the custom attach wrapper always fetches the target model manual. By fetching the target model (again) is a little performance decrease, but it also has advantages: we can register the event, and directly unregister the event again. So very clean code.
Important: please only use the custom attach wrapper in the future.
* 🎨 token model had overriden the onCreating function because of the created_at field
- we need to ensure that the base onCreating hook get's triggered for ALL models
- if not, they don't get an ObjectId assigned
- in this case: be smart and check if the target model has a created_at field
* 🎨 we don't have a uuid field anymore, remove the usages
- no default uuid creation in models
- i am pretty sure we have some more definitions in our tests (for example in the export json files), but that is too much work to delete them all
* 🎨 do not parse ID to Number
- we had various occurances of parsing all ID's to numbers
- we don't need this behaviour anymore
- ID is string
- i will adapt the ID validation in the next commit
* 🎨 change ID regex for validation
- we only allow: ID as ObjectId, ID as 1 and ID as me
- we need to keep ID 1, because our whole software relies on ID 1 (permissions etc)
* 🎨 owner fixture
- roles: [4] does not work anymore
- 4 means -> static id 4
- this worked in an auto increment system (not even in a system with distributed writes)
- with ObjectId we generate each ID automatically (for static and dynamic resources)
- it is possible to define all id's for static resources still, but that means we need to know which ID is already used and for consistency we have to define ObjectId's for these static resources
- so no static id's anymore, except of: id 1 for owner and id 0 for external usage (because this is required from our permission system)
- NOTE: please read through the comment in the user model
* 🎨 tests: DataGenerator and test utils
First of all: we need to ensure using ObjectId's in the tests. When don't, we can't ensure that ObjectId's work properly.
This commit brings lot's of dynamic into all the static defined id's.
In one of the next commits, i will adapt all the tests.
* 🚨 remove counter in Notification API
- no need to add a counter
- we simply generate ObjectId's (they are auto incremental as well)
- our id validator does only allow ObjectId as id,1 and me
* 🎨 extend contextUser in Base Model
- remove isNumber check, because id's are no longer numbers, except of id 0/1
- use existing isExternalUser
- support id 0/1 as string or number
* ✨ Ghost Owner has id 1
- ensure we define this id in the fixtures.json
- doesn't matter if number or string
* 🎨 functional tests adaptions
- use dynamic id's
* 🎨 fix unit tests
* 🎨 integration tests adaptions
* 🎨 change importer utils
- all our export examples (test/fixtures/exports) contain id's as numbers
- fact: but we ignore them anyway when inserting into the database, see https://github.com/TryGhost/Ghost/blob/master/core/server/data/import/utils.js#L249
- in https://github.com/TryGhost/Ghost/pull/7495/commits/0e6ed957cd54dc02a25cf6fb1ab7d7e723295e2c#diff-70f514a06347c048648be464819503c4L67 i removed parsing id's to integers
- i realised that this ^ check just existed, because the userIdToMap was an object key and object keys are always strings!
- i think this logic is a little bit complicated, but i don't want to refactor this now
- this commit ensures when trying to find the user, the id comparison works again
- i've added more documentation to understand this logic ;)
- plus i renamed an attribute to improve readability
* 🎨 Data-Generator: add more defaults to createUser
- if i use the function DataGenerator.forKnex.createUser i would like to get a full set of defaults
* 🎨 test utils: change/extend function set for functional tests
- functional tests work a bit different
- they boot Ghost and seed the database
- some functional tests have mis-used the test setup
- the test setup needs two sections: integration/unit and functional tests
- any functional test is allowed to either add more data or change data in the existing Ghost db
- but what it should not do is: add test fixtures like roles or users from our DataGenerator and cross fingers it will work
- this commit adds a clean method for functional tests to add extra users
* 🎨 functional tests adaptions
- use last commit to insert users for functional tests clean
- tidy up usage of testUtils.setup or testUtils.doAuth
* 🐛 test utils: reset database before init
- ensure we don't have any left data from other tests in the database when starting ghost
* 🐛 fix test (unrelated to this PR)
- fixes a random failure
- return statement was missing
* 🎨 make changes for invites
2016-11-17 12:09:11 +03:00
id : ObjectId . generate ( ) ,
2014-07-24 13:46:05 +04:00
email : 'a' + user . email ,
slug : 'a' + user . slug
} ) ) ;
} ) ;
✨ replace auto increment id's by object id (#7495)
* 🛠 bookshelf tarball, bson-objectid
* 🎨 schema changes
- change increment type to string
- add a default fallback for string length 191 (to avoid adding this logic to every single column which uses an ID)
- remove uuid, because ID now represents a global resource identifier
- keep uuid for post, because we are using this as preview id
- keep uuid for clients for now - we are using this param for Ghost-Auth
* ✨ base model: generate ObjectId on creating event
- each new resource get's a auto generate ObjectId
- this logic won't work for attached models, this commit comes later
* 🎨 centralised attach method
When attaching models there are two things important two know
1. To be able to attach an ObjectId, we need to register the `onCreating` event the fetched model!This is caused by the Bookshelf design in general. On this target model we are attaching the new model.
2. We need to manually fetch the target model, because Bookshelf has a weird behaviour (which is known as a bug, see see https://github.com/tgriesser/bookshelf/issues/629). The most important property when attaching a model is `parentFk`, which is the foreign key. This can be null when fetching the model with the option `withRelated`. To ensure quality and consistency, the custom attach wrapper always fetches the target model manual. By fetching the target model (again) is a little performance decrease, but it also has advantages: we can register the event, and directly unregister the event again. So very clean code.
Important: please only use the custom attach wrapper in the future.
* 🎨 token model had overriden the onCreating function because of the created_at field
- we need to ensure that the base onCreating hook get's triggered for ALL models
- if not, they don't get an ObjectId assigned
- in this case: be smart and check if the target model has a created_at field
* 🎨 we don't have a uuid field anymore, remove the usages
- no default uuid creation in models
- i am pretty sure we have some more definitions in our tests (for example in the export json files), but that is too much work to delete them all
* 🎨 do not parse ID to Number
- we had various occurances of parsing all ID's to numbers
- we don't need this behaviour anymore
- ID is string
- i will adapt the ID validation in the next commit
* 🎨 change ID regex for validation
- we only allow: ID as ObjectId, ID as 1 and ID as me
- we need to keep ID 1, because our whole software relies on ID 1 (permissions etc)
* 🎨 owner fixture
- roles: [4] does not work anymore
- 4 means -> static id 4
- this worked in an auto increment system (not even in a system with distributed writes)
- with ObjectId we generate each ID automatically (for static and dynamic resources)
- it is possible to define all id's for static resources still, but that means we need to know which ID is already used and for consistency we have to define ObjectId's for these static resources
- so no static id's anymore, except of: id 1 for owner and id 0 for external usage (because this is required from our permission system)
- NOTE: please read through the comment in the user model
* 🎨 tests: DataGenerator and test utils
First of all: we need to ensure using ObjectId's in the tests. When don't, we can't ensure that ObjectId's work properly.
This commit brings lot's of dynamic into all the static defined id's.
In one of the next commits, i will adapt all the tests.
* 🚨 remove counter in Notification API
- no need to add a counter
- we simply generate ObjectId's (they are auto incremental as well)
- our id validator does only allow ObjectId as id,1 and me
* 🎨 extend contextUser in Base Model
- remove isNumber check, because id's are no longer numbers, except of id 0/1
- use existing isExternalUser
- support id 0/1 as string or number
* ✨ Ghost Owner has id 1
- ensure we define this id in the fixtures.json
- doesn't matter if number or string
* 🎨 functional tests adaptions
- use dynamic id's
* 🎨 fix unit tests
* 🎨 integration tests adaptions
* 🎨 change importer utils
- all our export examples (test/fixtures/exports) contain id's as numbers
- fact: but we ignore them anyway when inserting into the database, see https://github.com/TryGhost/Ghost/blob/master/core/server/data/import/utils.js#L249
- in https://github.com/TryGhost/Ghost/pull/7495/commits/0e6ed957cd54dc02a25cf6fb1ab7d7e723295e2c#diff-70f514a06347c048648be464819503c4L67 i removed parsing id's to integers
- i realised that this ^ check just existed, because the userIdToMap was an object key and object keys are always strings!
- i think this logic is a little bit complicated, but i don't want to refactor this now
- this commit ensures when trying to find the user, the id comparison works again
- i've added more documentation to understand this logic ;)
- plus i renamed an attribute to improve readability
* 🎨 Data-Generator: add more defaults to createUser
- if i use the function DataGenerator.forKnex.createUser i would like to get a full set of defaults
* 🎨 test utils: change/extend function set for functional tests
- functional tests work a bit different
- they boot Ghost and seed the database
- some functional tests have mis-used the test setup
- the test setup needs two sections: integration/unit and functional tests
- any functional test is allowed to either add more data or change data in the existing Ghost db
- but what it should not do is: add test fixtures like roles or users from our DataGenerator and cross fingers it will work
- this commit adds a clean method for functional tests to add extra users
* 🎨 functional tests adaptions
- use last commit to insert users for functional tests clean
- tidy up usage of testUtils.setup or testUtils.doAuth
* 🐛 test utils: reset database before init
- ensure we don't have any left data from other tests in the database when starting ghost
* 🐛 fix test (unrelated to this PR)
- fixes a random failure
- return statement was missing
* 🎨 make changes for invites
2016-11-17 12:09:11 +03:00
// @TODO: remove when overhauling test env
// tests need access to the extra created users (especially to the created id)
// replacement for admin2, editor2 etc
DataGenerator . Content . extraUsers = extraUsers ;
2016-02-12 14:56:27 +03:00
return db . knex ( 'users' ) . insert ( extraUsers ) . then ( function ( ) {
return db . knex ( 'roles_users' ) . insert ( [
✨ replace auto increment id's by object id (#7495)
* 🛠 bookshelf tarball, bson-objectid
* 🎨 schema changes
- change increment type to string
- add a default fallback for string length 191 (to avoid adding this logic to every single column which uses an ID)
- remove uuid, because ID now represents a global resource identifier
- keep uuid for post, because we are using this as preview id
- keep uuid for clients for now - we are using this param for Ghost-Auth
* ✨ base model: generate ObjectId on creating event
- each new resource get's a auto generate ObjectId
- this logic won't work for attached models, this commit comes later
* 🎨 centralised attach method
When attaching models there are two things important two know
1. To be able to attach an ObjectId, we need to register the `onCreating` event the fetched model!This is caused by the Bookshelf design in general. On this target model we are attaching the new model.
2. We need to manually fetch the target model, because Bookshelf has a weird behaviour (which is known as a bug, see see https://github.com/tgriesser/bookshelf/issues/629). The most important property when attaching a model is `parentFk`, which is the foreign key. This can be null when fetching the model with the option `withRelated`. To ensure quality and consistency, the custom attach wrapper always fetches the target model manual. By fetching the target model (again) is a little performance decrease, but it also has advantages: we can register the event, and directly unregister the event again. So very clean code.
Important: please only use the custom attach wrapper in the future.
* 🎨 token model had overriden the onCreating function because of the created_at field
- we need to ensure that the base onCreating hook get's triggered for ALL models
- if not, they don't get an ObjectId assigned
- in this case: be smart and check if the target model has a created_at field
* 🎨 we don't have a uuid field anymore, remove the usages
- no default uuid creation in models
- i am pretty sure we have some more definitions in our tests (for example in the export json files), but that is too much work to delete them all
* 🎨 do not parse ID to Number
- we had various occurances of parsing all ID's to numbers
- we don't need this behaviour anymore
- ID is string
- i will adapt the ID validation in the next commit
* 🎨 change ID regex for validation
- we only allow: ID as ObjectId, ID as 1 and ID as me
- we need to keep ID 1, because our whole software relies on ID 1 (permissions etc)
* 🎨 owner fixture
- roles: [4] does not work anymore
- 4 means -> static id 4
- this worked in an auto increment system (not even in a system with distributed writes)
- with ObjectId we generate each ID automatically (for static and dynamic resources)
- it is possible to define all id's for static resources still, but that means we need to know which ID is already used and for consistency we have to define ObjectId's for these static resources
- so no static id's anymore, except of: id 1 for owner and id 0 for external usage (because this is required from our permission system)
- NOTE: please read through the comment in the user model
* 🎨 tests: DataGenerator and test utils
First of all: we need to ensure using ObjectId's in the tests. When don't, we can't ensure that ObjectId's work properly.
This commit brings lot's of dynamic into all the static defined id's.
In one of the next commits, i will adapt all the tests.
* 🚨 remove counter in Notification API
- no need to add a counter
- we simply generate ObjectId's (they are auto incremental as well)
- our id validator does only allow ObjectId as id,1 and me
* 🎨 extend contextUser in Base Model
- remove isNumber check, because id's are no longer numbers, except of id 0/1
- use existing isExternalUser
- support id 0/1 as string or number
* ✨ Ghost Owner has id 1
- ensure we define this id in the fixtures.json
- doesn't matter if number or string
* 🎨 functional tests adaptions
- use dynamic id's
* 🎨 fix unit tests
* 🎨 integration tests adaptions
* 🎨 change importer utils
- all our export examples (test/fixtures/exports) contain id's as numbers
- fact: but we ignore them anyway when inserting into the database, see https://github.com/TryGhost/Ghost/blob/master/core/server/data/import/utils.js#L249
- in https://github.com/TryGhost/Ghost/pull/7495/commits/0e6ed957cd54dc02a25cf6fb1ab7d7e723295e2c#diff-70f514a06347c048648be464819503c4L67 i removed parsing id's to integers
- i realised that this ^ check just existed, because the userIdToMap was an object key and object keys are always strings!
- i think this logic is a little bit complicated, but i don't want to refactor this now
- this commit ensures when trying to find the user, the id comparison works again
- i've added more documentation to understand this logic ;)
- plus i renamed an attribute to improve readability
* 🎨 Data-Generator: add more defaults to createUser
- if i use the function DataGenerator.forKnex.createUser i would like to get a full set of defaults
* 🎨 test utils: change/extend function set for functional tests
- functional tests work a bit different
- they boot Ghost and seed the database
- some functional tests have mis-used the test setup
- the test setup needs two sections: integration/unit and functional tests
- any functional test is allowed to either add more data or change data in the existing Ghost db
- but what it should not do is: add test fixtures like roles or users from our DataGenerator and cross fingers it will work
- this commit adds a clean method for functional tests to add extra users
* 🎨 functional tests adaptions
- use last commit to insert users for functional tests clean
- tidy up usage of testUtils.setup or testUtils.doAuth
* 🐛 test utils: reset database before init
- ensure we don't have any left data from other tests in the database when starting ghost
* 🐛 fix test (unrelated to this PR)
- fixes a random failure
- return statement was missing
* 🎨 make changes for invites
2016-11-17 12:09:11 +03:00
{ id : ObjectId . generate ( ) , user _id : extraUsers [ 0 ] . id , role _id : DataGenerator . Content . roles [ 0 ] . id } ,
{ id : ObjectId . generate ( ) , user _id : extraUsers [ 1 ] . id , role _id : DataGenerator . Content . roles [ 1 ] . id } ,
{ id : ObjectId . generate ( ) , user _id : extraUsers [ 2 ] . id , role _id : DataGenerator . Content . roles [ 2 ] . id }
2014-07-24 13:46:05 +04:00
] ) ;
} ) ;
} ,
🐛 be able to serve locked users (#8711)
closes #8645, closes #8710
- locked users were once part of the category "active users", but were moved to the inactive category
-> we have added a protection of not being able to edit yourself when you are either suspended or locked
- but they are not really active users, they are restricted, because they have no access to the admin panel
- support three categories: active, inactive, restricted
* - revert restricted states
- instead, update permission layer: fallback to `all` by default, because you are able to serve any user status
- add more tests
- ATTENTION: there is a behaviour change, that a blog owner's author page can be served before setting up the blog, see conversation on slack
-> LTS serves 404
-> 1.0 would serve 200
2017-07-20 14:45:13 +03:00
insertOneUser : function insertOneUser ( options ) {
options = options || { } ;
return db . knex ( 'users' ) . insert ( DataGenerator . forKnex . createUser ( {
email : options . email ,
slug : options . slug ,
status : options . status
} ) ) ;
} ,
2017-03-13 15:03:26 +03:00
// Creates a client, and access and refresh tokens for user with index or 2 by default
createTokensForUser : function createTokensForUser ( index ) {
2016-02-12 14:56:27 +03:00
return db . knex ( 'clients' ) . insert ( DataGenerator . forKnex . clients ) . then ( function ( ) {
2017-03-13 15:03:26 +03:00
return db . knex ( 'accesstokens' ) . insert ( DataGenerator . forKnex . createToken ( {
user _id : DataGenerator . Content . users [ index || 2 ] . id
} ) ) ;
2014-08-23 21:59:36 +04:00
} ) . then ( function ( ) {
2017-03-13 15:03:26 +03:00
return db . knex ( 'refreshtokens' ) . insert ( DataGenerator . forKnex . createToken ( {
user _id : DataGenerator . Content . users [ index || 2 ] . id
} ) ) ;
2014-08-05 22:11:17 +04:00
} ) ;
} ,
2016-06-10 08:12:46 +03:00
insertOne : function insertOne ( obj , fn , index ) {
2016-02-12 14:56:27 +03:00
return db . knex ( obj )
2017-03-09 22:38:20 +03:00
. insert ( DataGenerator . forKnex [ fn ] ( DataGenerator . Content [ obj ] [ index || 0 ] ) ) ;
2014-07-21 21:50:04 +04:00
} ,
insertApps : function insertApps ( ) {
2016-02-12 14:56:27 +03:00
return db . knex ( 'apps' ) . insert ( DataGenerator . forKnex . apps ) . then ( function ( ) {
return db . knex ( 'app_fields' ) . insert ( DataGenerator . forKnex . app _fields ) ;
2014-07-21 21:50:04 +04:00
} ) ;
} ,
2014-12-14 14:31:00 +03:00
getImportFixturePath : function ( filename ) {
return path . resolve ( _ _dirname + '/fixtures/import/' + filename ) ;
} ,
2017-06-04 12:53:00 +03:00
getExportFixturePath : function ( filename , options ) {
options = options || { lts : false } ;
var relativePath = options . lts ? '/fixtures/export/lts/' : '/fixtures/export/' ;
return path . resolve ( _ _dirname + relativePath + filename + '.json' ) ;
2014-12-11 18:50:10 +03:00
} ,
2017-06-04 12:53:00 +03:00
loadExportFixture : function loadExportFixture ( filename , options ) {
options = options || { lts : false } ;
var filePath = this . getExportFixturePath ( filename , options ) ,
2014-08-17 10:17:23 +04:00
readFile = Promise . promisify ( fs . readFile ) ;
2014-07-21 21:50:04 +04:00
2014-12-11 18:50:10 +03:00
return readFile ( filePath ) . then ( function ( fileContents ) {
2014-07-21 21:50:04 +04:00
var data ;
// Parse the json data
try {
data = JSON . parse ( fileContents ) ;
} catch ( e ) {
2014-08-17 10:17:23 +04:00
return new Error ( 'Failed to parse the file' ) ;
2014-02-25 00:28:18 +04:00
}
2014-07-21 21:50:04 +04:00
return data ;
} ) ;
} ,
permissionsFor : function permissionsFor ( obj ) {
2016-04-08 12:09:26 +03:00
var permsToInsert = fixtureUtils . findModelFixtures ( 'Permission' , { object _type : obj } ) . entries ,
2016-03-29 16:14:44 +03:00
permsRolesToInsert = fixtureUtils . findPermissionRelationsForObject ( obj ) . entries ,
2014-07-21 21:50:04 +04:00
actions = [ ] ,
2014-09-10 08:06:24 +04:00
permissionsRoles = [ ] ,
2014-07-21 21:50:04 +04:00
roles = {
✨ replace auto increment id's by object id (#7495)
* 🛠 bookshelf tarball, bson-objectid
* 🎨 schema changes
- change increment type to string
- add a default fallback for string length 191 (to avoid adding this logic to every single column which uses an ID)
- remove uuid, because ID now represents a global resource identifier
- keep uuid for post, because we are using this as preview id
- keep uuid for clients for now - we are using this param for Ghost-Auth
* ✨ base model: generate ObjectId on creating event
- each new resource get's a auto generate ObjectId
- this logic won't work for attached models, this commit comes later
* 🎨 centralised attach method
When attaching models there are two things important two know
1. To be able to attach an ObjectId, we need to register the `onCreating` event the fetched model!This is caused by the Bookshelf design in general. On this target model we are attaching the new model.
2. We need to manually fetch the target model, because Bookshelf has a weird behaviour (which is known as a bug, see see https://github.com/tgriesser/bookshelf/issues/629). The most important property when attaching a model is `parentFk`, which is the foreign key. This can be null when fetching the model with the option `withRelated`. To ensure quality and consistency, the custom attach wrapper always fetches the target model manual. By fetching the target model (again) is a little performance decrease, but it also has advantages: we can register the event, and directly unregister the event again. So very clean code.
Important: please only use the custom attach wrapper in the future.
* 🎨 token model had overriden the onCreating function because of the created_at field
- we need to ensure that the base onCreating hook get's triggered for ALL models
- if not, they don't get an ObjectId assigned
- in this case: be smart and check if the target model has a created_at field
* 🎨 we don't have a uuid field anymore, remove the usages
- no default uuid creation in models
- i am pretty sure we have some more definitions in our tests (for example in the export json files), but that is too much work to delete them all
* 🎨 do not parse ID to Number
- we had various occurances of parsing all ID's to numbers
- we don't need this behaviour anymore
- ID is string
- i will adapt the ID validation in the next commit
* 🎨 change ID regex for validation
- we only allow: ID as ObjectId, ID as 1 and ID as me
- we need to keep ID 1, because our whole software relies on ID 1 (permissions etc)
* 🎨 owner fixture
- roles: [4] does not work anymore
- 4 means -> static id 4
- this worked in an auto increment system (not even in a system with distributed writes)
- with ObjectId we generate each ID automatically (for static and dynamic resources)
- it is possible to define all id's for static resources still, but that means we need to know which ID is already used and for consistency we have to define ObjectId's for these static resources
- so no static id's anymore, except of: id 1 for owner and id 0 for external usage (because this is required from our permission system)
- NOTE: please read through the comment in the user model
* 🎨 tests: DataGenerator and test utils
First of all: we need to ensure using ObjectId's in the tests. When don't, we can't ensure that ObjectId's work properly.
This commit brings lot's of dynamic into all the static defined id's.
In one of the next commits, i will adapt all the tests.
* 🚨 remove counter in Notification API
- no need to add a counter
- we simply generate ObjectId's (they are auto incremental as well)
- our id validator does only allow ObjectId as id,1 and me
* 🎨 extend contextUser in Base Model
- remove isNumber check, because id's are no longer numbers, except of id 0/1
- use existing isExternalUser
- support id 0/1 as string or number
* ✨ Ghost Owner has id 1
- ensure we define this id in the fixtures.json
- doesn't matter if number or string
* 🎨 functional tests adaptions
- use dynamic id's
* 🎨 fix unit tests
* 🎨 integration tests adaptions
* 🎨 change importer utils
- all our export examples (test/fixtures/exports) contain id's as numbers
- fact: but we ignore them anyway when inserting into the database, see https://github.com/TryGhost/Ghost/blob/master/core/server/data/import/utils.js#L249
- in https://github.com/TryGhost/Ghost/pull/7495/commits/0e6ed957cd54dc02a25cf6fb1ab7d7e723295e2c#diff-70f514a06347c048648be464819503c4L67 i removed parsing id's to integers
- i realised that this ^ check just existed, because the userIdToMap was an object key and object keys are always strings!
- i think this logic is a little bit complicated, but i don't want to refactor this now
- this commit ensures when trying to find the user, the id comparison works again
- i've added more documentation to understand this logic ;)
- plus i renamed an attribute to improve readability
* 🎨 Data-Generator: add more defaults to createUser
- if i use the function DataGenerator.forKnex.createUser i would like to get a full set of defaults
* 🎨 test utils: change/extend function set for functional tests
- functional tests work a bit different
- they boot Ghost and seed the database
- some functional tests have mis-used the test setup
- the test setup needs two sections: integration/unit and functional tests
- any functional test is allowed to either add more data or change data in the existing Ghost db
- but what it should not do is: add test fixtures like roles or users from our DataGenerator and cross fingers it will work
- this commit adds a clean method for functional tests to add extra users
* 🎨 functional tests adaptions
- use last commit to insert users for functional tests clean
- tidy up usage of testUtils.setup or testUtils.doAuth
* 🐛 test utils: reset database before init
- ensure we don't have any left data from other tests in the database when starting ghost
* 🐛 fix test (unrelated to this PR)
- fixes a random failure
- return statement was missing
* 🎨 make changes for invites
2016-11-17 12:09:11 +03:00
Administrator : DataGenerator . Content . roles [ 0 ] . id ,
Editor : DataGenerator . Content . roles [ 1 ] . id ,
Author : DataGenerator . Content . roles [ 2 ] . id ,
Owner : DataGenerator . Content . roles [ 3 ] . id
2014-07-21 21:50:04 +04:00
} ;
2016-05-25 10:34:46 +03:00
// CASE: if empty db will throw SQLITE_MISUSE, hard to debug
if ( _ . isEmpty ( permsToInsert ) ) {
return Promise . reject ( new Error ( 'no permission found:' + obj ) ) ;
}
2014-07-21 21:50:04 +04:00
permsToInsert = _ . map ( permsToInsert , function ( perms ) {
✨ replace auto increment id's by object id (#7495)
* 🛠 bookshelf tarball, bson-objectid
* 🎨 schema changes
- change increment type to string
- add a default fallback for string length 191 (to avoid adding this logic to every single column which uses an ID)
- remove uuid, because ID now represents a global resource identifier
- keep uuid for post, because we are using this as preview id
- keep uuid for clients for now - we are using this param for Ghost-Auth
* ✨ base model: generate ObjectId on creating event
- each new resource get's a auto generate ObjectId
- this logic won't work for attached models, this commit comes later
* 🎨 centralised attach method
When attaching models there are two things important two know
1. To be able to attach an ObjectId, we need to register the `onCreating` event the fetched model!This is caused by the Bookshelf design in general. On this target model we are attaching the new model.
2. We need to manually fetch the target model, because Bookshelf has a weird behaviour (which is known as a bug, see see https://github.com/tgriesser/bookshelf/issues/629). The most important property when attaching a model is `parentFk`, which is the foreign key. This can be null when fetching the model with the option `withRelated`. To ensure quality and consistency, the custom attach wrapper always fetches the target model manual. By fetching the target model (again) is a little performance decrease, but it also has advantages: we can register the event, and directly unregister the event again. So very clean code.
Important: please only use the custom attach wrapper in the future.
* 🎨 token model had overriden the onCreating function because of the created_at field
- we need to ensure that the base onCreating hook get's triggered for ALL models
- if not, they don't get an ObjectId assigned
- in this case: be smart and check if the target model has a created_at field
* 🎨 we don't have a uuid field anymore, remove the usages
- no default uuid creation in models
- i am pretty sure we have some more definitions in our tests (for example in the export json files), but that is too much work to delete them all
* 🎨 do not parse ID to Number
- we had various occurances of parsing all ID's to numbers
- we don't need this behaviour anymore
- ID is string
- i will adapt the ID validation in the next commit
* 🎨 change ID regex for validation
- we only allow: ID as ObjectId, ID as 1 and ID as me
- we need to keep ID 1, because our whole software relies on ID 1 (permissions etc)
* 🎨 owner fixture
- roles: [4] does not work anymore
- 4 means -> static id 4
- this worked in an auto increment system (not even in a system with distributed writes)
- with ObjectId we generate each ID automatically (for static and dynamic resources)
- it is possible to define all id's for static resources still, but that means we need to know which ID is already used and for consistency we have to define ObjectId's for these static resources
- so no static id's anymore, except of: id 1 for owner and id 0 for external usage (because this is required from our permission system)
- NOTE: please read through the comment in the user model
* 🎨 tests: DataGenerator and test utils
First of all: we need to ensure using ObjectId's in the tests. When don't, we can't ensure that ObjectId's work properly.
This commit brings lot's of dynamic into all the static defined id's.
In one of the next commits, i will adapt all the tests.
* 🚨 remove counter in Notification API
- no need to add a counter
- we simply generate ObjectId's (they are auto incremental as well)
- our id validator does only allow ObjectId as id,1 and me
* 🎨 extend contextUser in Base Model
- remove isNumber check, because id's are no longer numbers, except of id 0/1
- use existing isExternalUser
- support id 0/1 as string or number
* ✨ Ghost Owner has id 1
- ensure we define this id in the fixtures.json
- doesn't matter if number or string
* 🎨 functional tests adaptions
- use dynamic id's
* 🎨 fix unit tests
* 🎨 integration tests adaptions
* 🎨 change importer utils
- all our export examples (test/fixtures/exports) contain id's as numbers
- fact: but we ignore them anyway when inserting into the database, see https://github.com/TryGhost/Ghost/blob/master/core/server/data/import/utils.js#L249
- in https://github.com/TryGhost/Ghost/pull/7495/commits/0e6ed957cd54dc02a25cf6fb1ab7d7e723295e2c#diff-70f514a06347c048648be464819503c4L67 i removed parsing id's to integers
- i realised that this ^ check just existed, because the userIdToMap was an object key and object keys are always strings!
- i think this logic is a little bit complicated, but i don't want to refactor this now
- this commit ensures when trying to find the user, the id comparison works again
- i've added more documentation to understand this logic ;)
- plus i renamed an attribute to improve readability
* 🎨 Data-Generator: add more defaults to createUser
- if i use the function DataGenerator.forKnex.createUser i would like to get a full set of defaults
* 🎨 test utils: change/extend function set for functional tests
- functional tests work a bit different
- they boot Ghost and seed the database
- some functional tests have mis-used the test setup
- the test setup needs two sections: integration/unit and functional tests
- any functional test is allowed to either add more data or change data in the existing Ghost db
- but what it should not do is: add test fixtures like roles or users from our DataGenerator and cross fingers it will work
- this commit adds a clean method for functional tests to add extra users
* 🎨 functional tests adaptions
- use last commit to insert users for functional tests clean
- tidy up usage of testUtils.setup or testUtils.doAuth
* 🐛 test utils: reset database before init
- ensure we don't have any left data from other tests in the database when starting ghost
* 🐛 fix test (unrelated to this PR)
- fixes a random failure
- return statement was missing
* 🎨 make changes for invites
2016-11-17 12:09:11 +03:00
perms . id = ObjectId . generate ( ) ;
actions . push ( { type : perms . action _type , permissionId : perms . id } ) ;
2014-07-21 21:50:04 +04:00
return DataGenerator . forKnex . createBasic ( perms ) ;
2014-02-25 00:28:18 +04:00
} ) ;
2014-07-21 21:50:04 +04:00
_ . each ( permsRolesToInsert , function ( perms , role ) {
if ( perms [ obj ] ) {
if ( perms [ obj ] === 'all' ) {
✨ replace auto increment id's by object id (#7495)
* 🛠 bookshelf tarball, bson-objectid
* 🎨 schema changes
- change increment type to string
- add a default fallback for string length 191 (to avoid adding this logic to every single column which uses an ID)
- remove uuid, because ID now represents a global resource identifier
- keep uuid for post, because we are using this as preview id
- keep uuid for clients for now - we are using this param for Ghost-Auth
* ✨ base model: generate ObjectId on creating event
- each new resource get's a auto generate ObjectId
- this logic won't work for attached models, this commit comes later
* 🎨 centralised attach method
When attaching models there are two things important two know
1. To be able to attach an ObjectId, we need to register the `onCreating` event the fetched model!This is caused by the Bookshelf design in general. On this target model we are attaching the new model.
2. We need to manually fetch the target model, because Bookshelf has a weird behaviour (which is known as a bug, see see https://github.com/tgriesser/bookshelf/issues/629). The most important property when attaching a model is `parentFk`, which is the foreign key. This can be null when fetching the model with the option `withRelated`. To ensure quality and consistency, the custom attach wrapper always fetches the target model manual. By fetching the target model (again) is a little performance decrease, but it also has advantages: we can register the event, and directly unregister the event again. So very clean code.
Important: please only use the custom attach wrapper in the future.
* 🎨 token model had overriden the onCreating function because of the created_at field
- we need to ensure that the base onCreating hook get's triggered for ALL models
- if not, they don't get an ObjectId assigned
- in this case: be smart and check if the target model has a created_at field
* 🎨 we don't have a uuid field anymore, remove the usages
- no default uuid creation in models
- i am pretty sure we have some more definitions in our tests (for example in the export json files), but that is too much work to delete them all
* 🎨 do not parse ID to Number
- we had various occurances of parsing all ID's to numbers
- we don't need this behaviour anymore
- ID is string
- i will adapt the ID validation in the next commit
* 🎨 change ID regex for validation
- we only allow: ID as ObjectId, ID as 1 and ID as me
- we need to keep ID 1, because our whole software relies on ID 1 (permissions etc)
* 🎨 owner fixture
- roles: [4] does not work anymore
- 4 means -> static id 4
- this worked in an auto increment system (not even in a system with distributed writes)
- with ObjectId we generate each ID automatically (for static and dynamic resources)
- it is possible to define all id's for static resources still, but that means we need to know which ID is already used and for consistency we have to define ObjectId's for these static resources
- so no static id's anymore, except of: id 1 for owner and id 0 for external usage (because this is required from our permission system)
- NOTE: please read through the comment in the user model
* 🎨 tests: DataGenerator and test utils
First of all: we need to ensure using ObjectId's in the tests. When don't, we can't ensure that ObjectId's work properly.
This commit brings lot's of dynamic into all the static defined id's.
In one of the next commits, i will adapt all the tests.
* 🚨 remove counter in Notification API
- no need to add a counter
- we simply generate ObjectId's (they are auto incremental as well)
- our id validator does only allow ObjectId as id,1 and me
* 🎨 extend contextUser in Base Model
- remove isNumber check, because id's are no longer numbers, except of id 0/1
- use existing isExternalUser
- support id 0/1 as string or number
* ✨ Ghost Owner has id 1
- ensure we define this id in the fixtures.json
- doesn't matter if number or string
* 🎨 functional tests adaptions
- use dynamic id's
* 🎨 fix unit tests
* 🎨 integration tests adaptions
* 🎨 change importer utils
- all our export examples (test/fixtures/exports) contain id's as numbers
- fact: but we ignore them anyway when inserting into the database, see https://github.com/TryGhost/Ghost/blob/master/core/server/data/import/utils.js#L249
- in https://github.com/TryGhost/Ghost/pull/7495/commits/0e6ed957cd54dc02a25cf6fb1ab7d7e723295e2c#diff-70f514a06347c048648be464819503c4L67 i removed parsing id's to integers
- i realised that this ^ check just existed, because the userIdToMap was an object key and object keys are always strings!
- i think this logic is a little bit complicated, but i don't want to refactor this now
- this commit ensures when trying to find the user, the id comparison works again
- i've added more documentation to understand this logic ;)
- plus i renamed an attribute to improve readability
* 🎨 Data-Generator: add more defaults to createUser
- if i use the function DataGenerator.forKnex.createUser i would like to get a full set of defaults
* 🎨 test utils: change/extend function set for functional tests
- functional tests work a bit different
- they boot Ghost and seed the database
- some functional tests have mis-used the test setup
- the test setup needs two sections: integration/unit and functional tests
- any functional test is allowed to either add more data or change data in the existing Ghost db
- but what it should not do is: add test fixtures like roles or users from our DataGenerator and cross fingers it will work
- this commit adds a clean method for functional tests to add extra users
* 🎨 functional tests adaptions
- use last commit to insert users for functional tests clean
- tidy up usage of testUtils.setup or testUtils.doAuth
* 🐛 test utils: reset database before init
- ensure we don't have any left data from other tests in the database when starting ghost
* 🐛 fix test (unrelated to this PR)
- fixes a random failure
- return statement was missing
* 🎨 make changes for invites
2016-11-17 12:09:11 +03:00
_ . each ( actions , function ( action ) {
permissionsRoles . push ( {
id : ObjectId . generate ( ) ,
permission _id : action . permissionId ,
role _id : roles [ role ]
} ) ;
2014-07-21 21:50:04 +04:00
} ) ;
2014-09-10 08:06:24 +04:00
} else {
2014-07-21 21:50:04 +04:00
_ . each ( perms [ obj ] , function ( action ) {
✨ replace auto increment id's by object id (#7495)
* 🛠 bookshelf tarball, bson-objectid
* 🎨 schema changes
- change increment type to string
- add a default fallback for string length 191 (to avoid adding this logic to every single column which uses an ID)
- remove uuid, because ID now represents a global resource identifier
- keep uuid for post, because we are using this as preview id
- keep uuid for clients for now - we are using this param for Ghost-Auth
* ✨ base model: generate ObjectId on creating event
- each new resource get's a auto generate ObjectId
- this logic won't work for attached models, this commit comes later
* 🎨 centralised attach method
When attaching models there are two things important two know
1. To be able to attach an ObjectId, we need to register the `onCreating` event the fetched model!This is caused by the Bookshelf design in general. On this target model we are attaching the new model.
2. We need to manually fetch the target model, because Bookshelf has a weird behaviour (which is known as a bug, see see https://github.com/tgriesser/bookshelf/issues/629). The most important property when attaching a model is `parentFk`, which is the foreign key. This can be null when fetching the model with the option `withRelated`. To ensure quality and consistency, the custom attach wrapper always fetches the target model manual. By fetching the target model (again) is a little performance decrease, but it also has advantages: we can register the event, and directly unregister the event again. So very clean code.
Important: please only use the custom attach wrapper in the future.
* 🎨 token model had overriden the onCreating function because of the created_at field
- we need to ensure that the base onCreating hook get's triggered for ALL models
- if not, they don't get an ObjectId assigned
- in this case: be smart and check if the target model has a created_at field
* 🎨 we don't have a uuid field anymore, remove the usages
- no default uuid creation in models
- i am pretty sure we have some more definitions in our tests (for example in the export json files), but that is too much work to delete them all
* 🎨 do not parse ID to Number
- we had various occurances of parsing all ID's to numbers
- we don't need this behaviour anymore
- ID is string
- i will adapt the ID validation in the next commit
* 🎨 change ID regex for validation
- we only allow: ID as ObjectId, ID as 1 and ID as me
- we need to keep ID 1, because our whole software relies on ID 1 (permissions etc)
* 🎨 owner fixture
- roles: [4] does not work anymore
- 4 means -> static id 4
- this worked in an auto increment system (not even in a system with distributed writes)
- with ObjectId we generate each ID automatically (for static and dynamic resources)
- it is possible to define all id's for static resources still, but that means we need to know which ID is already used and for consistency we have to define ObjectId's for these static resources
- so no static id's anymore, except of: id 1 for owner and id 0 for external usage (because this is required from our permission system)
- NOTE: please read through the comment in the user model
* 🎨 tests: DataGenerator and test utils
First of all: we need to ensure using ObjectId's in the tests. When don't, we can't ensure that ObjectId's work properly.
This commit brings lot's of dynamic into all the static defined id's.
In one of the next commits, i will adapt all the tests.
* 🚨 remove counter in Notification API
- no need to add a counter
- we simply generate ObjectId's (they are auto incremental as well)
- our id validator does only allow ObjectId as id,1 and me
* 🎨 extend contextUser in Base Model
- remove isNumber check, because id's are no longer numbers, except of id 0/1
- use existing isExternalUser
- support id 0/1 as string or number
* ✨ Ghost Owner has id 1
- ensure we define this id in the fixtures.json
- doesn't matter if number or string
* 🎨 functional tests adaptions
- use dynamic id's
* 🎨 fix unit tests
* 🎨 integration tests adaptions
* 🎨 change importer utils
- all our export examples (test/fixtures/exports) contain id's as numbers
- fact: but we ignore them anyway when inserting into the database, see https://github.com/TryGhost/Ghost/blob/master/core/server/data/import/utils.js#L249
- in https://github.com/TryGhost/Ghost/pull/7495/commits/0e6ed957cd54dc02a25cf6fb1ab7d7e723295e2c#diff-70f514a06347c048648be464819503c4L67 i removed parsing id's to integers
- i realised that this ^ check just existed, because the userIdToMap was an object key and object keys are always strings!
- i think this logic is a little bit complicated, but i don't want to refactor this now
- this commit ensures when trying to find the user, the id comparison works again
- i've added more documentation to understand this logic ;)
- plus i renamed an attribute to improve readability
* 🎨 Data-Generator: add more defaults to createUser
- if i use the function DataGenerator.forKnex.createUser i would like to get a full set of defaults
* 🎨 test utils: change/extend function set for functional tests
- functional tests work a bit different
- they boot Ghost and seed the database
- some functional tests have mis-used the test setup
- the test setup needs two sections: integration/unit and functional tests
- any functional test is allowed to either add more data or change data in the existing Ghost db
- but what it should not do is: add test fixtures like roles or users from our DataGenerator and cross fingers it will work
- this commit adds a clean method for functional tests to add extra users
* 🎨 functional tests adaptions
- use last commit to insert users for functional tests clean
- tidy up usage of testUtils.setup or testUtils.doAuth
* 🐛 test utils: reset database before init
- ensure we don't have any left data from other tests in the database when starting ghost
* 🐛 fix test (unrelated to this PR)
- fixes a random failure
- return statement was missing
* 🎨 make changes for invites
2016-11-17 12:09:11 +03:00
permissionsRoles . push ( {
id : ObjectId . generate ( ) ,
permission _id : _ . find ( actions , { type : action } ) . permissionId ,
role _id : roles [ role ]
} ) ;
2014-07-21 21:50:04 +04:00
} ) ;
}
2014-02-25 00:28:18 +04:00
}
2014-07-21 21:50:04 +04:00
} ) ;
2014-02-25 00:28:18 +04:00
2016-02-12 14:56:27 +03:00
return db . knex ( 'permissions' ) . insert ( permsToInsert ) . then ( function ( ) {
2016-05-25 10:34:46 +03:00
if ( _ . isEmpty ( permissionsRoles ) ) {
return Promise . resolve ( ) ;
}
2016-02-12 14:56:27 +03:00
return db . knex ( 'permissions_roles' ) . insert ( permissionsRoles ) ;
2014-02-25 00:28:18 +04:00
} ) ;
2015-08-22 00:46:42 +03:00
} ,
2016-05-25 10:34:46 +03:00
2015-08-22 00:46:42 +03:00
insertClients : function insertClients ( ) {
2016-02-12 14:56:27 +03:00
return db . knex ( 'clients' ) . insert ( DataGenerator . forKnex . clients ) ;
2015-08-22 00:46:42 +03:00
} ,
2016-05-25 10:34:46 +03:00
2017-03-03 19:41:20 +03:00
insertClientWithTrustedDomain : function insertClientWithTrustedDomain ( ) {
var client = DataGenerator . forKnex . createClient ( { slug : 'ghost-test' } ) ;
return db . knex ( 'clients' )
. insert ( client )
. then ( function ( ) {
return db . knex ( 'client_trusted_domains' )
. insert ( DataGenerator . forKnex . createTrustedDomain ( { client _id : client . id } ) ) ;
} ) ;
} ,
2015-08-22 00:46:42 +03:00
insertAccessToken : function insertAccessToken ( override ) {
2016-02-12 14:56:27 +03:00
return db . knex ( 'accesstokens' ) . insert ( DataGenerator . forKnex . createToken ( override ) ) ;
2016-09-21 17:48:14 +03:00
} ,
insertInvites : function insertInvites ( ) {
2016-11-16 12:33:44 +03:00
return db . knex ( 'invites' ) . insert ( DataGenerator . forKnex . invites ) ;
2017-11-21 18:43:14 +03:00
} ,
insertWebhooks : function insertWebhooks ( ) {
return db . knex ( 'webhooks' ) . insert ( DataGenerator . forKnex . webhooks ) ;
2014-07-21 21:50:04 +04:00
}
} ;
2014-02-25 00:28:18 +04:00
2014-07-21 21:50:04 +04:00
/** Test Utility Functions **/
initData = function initData ( ) {
2016-10-17 15:50:29 +03:00
return knexMigrator . init ( ) ;
2014-07-21 21:50:04 +04:00
} ;
2014-02-25 00:28:18 +04:00
2016-11-08 14:33:19 +03:00
clearBruteData = function clearBruteData ( ) {
return db . knex ( 'brute' ) . truncate ( ) ;
} ;
2016-11-07 14:39:49 +03:00
// we must always try to delete all tables
2014-07-21 21:50:04 +04:00
clearData = function clearData ( ) {
2016-11-07 14:39:49 +03:00
debug ( 'Database reset' ) ;
return knexMigrator . reset ( ) ;
2014-07-21 21:50:04 +04:00
} ;
2013-12-29 00:13:47 +04:00
2014-07-21 21:50:04 +04:00
toDoList = {
2017-03-09 22:38:20 +03:00
app : function insertApp ( ) {
return fixtures . insertOne ( 'apps' , 'createApp' ) ;
} ,
2014-09-10 08:06:24 +04:00
app _field : function insertAppField ( ) {
2014-07-21 21:50:04 +04:00
// TODO: use the actual app ID to create the field
return fixtures . insertOne ( 'apps' , 'createApp' ) . then ( function ( ) {
return fixtures . insertOne ( 'app_fields' , 'createAppField' ) ;
} ) ;
} ,
2014-09-10 08:06:24 +04:00
app _setting : function insertAppSetting ( ) {
2014-07-21 21:50:04 +04:00
// TODO: use the actual app ID to create the field
return fixtures . insertOne ( 'apps' , 'createApp' ) . then ( function ( ) {
return fixtures . insertOne ( 'app_settings' , 'createAppSetting' ) ;
} ) ;
} ,
2017-03-09 22:38:20 +03:00
permission : function insertPermission ( ) {
return fixtures . insertOne ( 'permissions' , 'createPermission' ) ;
} ,
role : function insertRole ( ) {
return fixtures . insertOne ( 'roles' , 'createRole' ) ;
} ,
roles : function insertRoles ( ) {
return fixtures . insertRoles ( ) ;
} ,
tag : function insertTag ( ) {
return fixtures . insertOne ( 'tags' , 'createTag' ) ;
} ,
subscriber : function insertSubscriber ( ) {
return fixtures . insertOne ( 'subscribers' , 'createSubscriber' ) ;
} ,
posts : function insertPostsAndTags ( ) {
return fixtures . insertPostsAndTags ( ) ;
} ,
'posts:mu' : function insertMultiAuthorPosts ( ) {
return fixtures . insertMultiAuthorPosts ( ) ;
} ,
tags : function insertMoreTags ( ) {
return fixtures . insertMoreTags ( ) ;
} ,
apps : function insertApps ( ) {
return fixtures . insertApps ( ) ;
} ,
settings : function populateSettings ( ) {
return SettingsLib . init ( ) ;
} ,
'users:roles' : function createUsersWithRoles ( ) {
return fixtures . createUsersWithRoles ( ) ;
} ,
'users:no-owner' : function createUsersWithoutOwner ( ) {
return fixtures . createUsersWithoutOwner ( ) ;
} ,
users : function createExtraUsers ( ) {
return fixtures . createExtraUsers ( ) ;
} ,
2017-03-13 15:03:26 +03:00
'user-token' : function createTokensForUser ( index ) {
return fixtures . createTokensForUser ( index ) ;
2017-03-09 22:38:20 +03:00
} ,
owner : function insertOwnerUser ( ) {
return fixtures . insertOwnerUser ( ) ;
} ,
'owner:pre' : function initOwnerUser ( ) {
return fixtures . initOwnerUser ( ) ;
} ,
'owner:post' : function overrideOwnerUser ( ) {
return fixtures . overrideOwnerUser ( ) ;
} ,
'perms:init' : function initPermissions ( ) {
return permissions . init ( ) ;
} ,
2014-09-10 08:06:24 +04:00
perms : function permissionsFor ( obj ) {
2017-03-13 15:03:26 +03:00
return fixtures . permissionsFor ( obj ) ;
2017-03-09 22:38:20 +03:00
} ,
clients : function insertClients ( ) {
return fixtures . insertClients ( ) ;
} ,
'client:trusted-domain' : function insertClients ( ) {
return fixtures . insertClientWithTrustedDomain ( ) ;
2015-08-22 00:46:42 +03:00
} ,
2017-03-09 22:38:20 +03:00
filter : function createFilterParamFixtures ( ) {
return filterData ( DataGenerator ) ;
} ,
invites : function insertInvites ( ) {
return fixtures . insertInvites ( ) ;
} ,
themes : function loadThemes ( ) {
return themes . loadAll ( ) ;
2017-11-21 18:43:14 +03:00
} ,
webhooks : function insertWebhooks ( ) {
return fixtures . insertWebhooks ( ) ;
2017-03-09 22:38:20 +03:00
}
2014-07-21 21:50:04 +04:00
} ;
2013-12-29 00:13:47 +04:00
2014-07-21 21:50:04 +04:00
/ * *
* # # getFixtureOps
*
* Takes the arguments from a setup function and turns them into an array of promises to fullfil
*
* This is effectively a list of instructions with regard to which fixtures should be setup for this test .
2017-03-09 22:38:20 +03:00
* * ` default ` - a special option which will cause the full suite of normal fixtures to be initialised
* * ` perms:init ` - initialise the permissions object after having added permissions
* * ` perms:obj ` - initialise permissions for a particular object type
* * ` users:roles ` - create a full suite of users , one per role
2014-09-10 08:06:24 +04:00
* @ param { Object } toDos
2014-07-21 21:50:04 +04:00
* /
getFixtureOps = function getFixtureOps ( toDos ) {
// default = default fixtures, if it isn't present, init with tables only
var tablesOnly = ! toDos . default ,
fixtureOps = [ ] ;
// Database initialisation
if ( toDos . init || toDos . default ) {
fixtureOps . push ( function initDB ( ) {
2016-10-12 18:18:57 +03:00
// skip adding all fixtures!
if ( tablesOnly ) {
2016-10-17 15:50:29 +03:00
return knexMigrator . init ( { skip : 2 } ) ;
2016-10-12 18:18:57 +03:00
}
2016-10-17 15:50:29 +03:00
return knexMigrator . init ( ) ;
2014-07-21 21:50:04 +04:00
} ) ;
2016-05-19 14:49:22 +03:00
2014-07-21 21:50:04 +04:00
delete toDos . default ;
delete toDos . init ;
}
2013-12-29 00:13:47 +04:00
2014-07-21 21:50:04 +04:00
// Go through our list of things to do, and add them to an array
_ . each ( toDos , function ( value , toDo ) {
var tmp ;
2016-05-25 10:34:46 +03:00
2017-03-13 15:03:26 +03:00
if ( ( toDo !== 'perms:init' && toDo . indexOf ( 'perms:' ) !== - 1 ) || toDo . indexOf ( 'user-token:' ) !== - 1 ) {
2014-07-21 21:50:04 +04:00
tmp = toDo . split ( ':' ) ;
2017-03-13 15:03:26 +03:00
fixtureOps . push ( function addCustomFixture ( ) {
return toDoList [ tmp [ 0 ] ] ( tmp [ 1 ] ) ;
} ) ;
2014-07-21 21:50:04 +04:00
} else {
2016-05-25 10:34:46 +03:00
if ( ! toDoList [ toDo ] ) {
✨ replace auto increment id's by object id (#7495)
* 🛠 bookshelf tarball, bson-objectid
* 🎨 schema changes
- change increment type to string
- add a default fallback for string length 191 (to avoid adding this logic to every single column which uses an ID)
- remove uuid, because ID now represents a global resource identifier
- keep uuid for post, because we are using this as preview id
- keep uuid for clients for now - we are using this param for Ghost-Auth
* ✨ base model: generate ObjectId on creating event
- each new resource get's a auto generate ObjectId
- this logic won't work for attached models, this commit comes later
* 🎨 centralised attach method
When attaching models there are two things important two know
1. To be able to attach an ObjectId, we need to register the `onCreating` event the fetched model!This is caused by the Bookshelf design in general. On this target model we are attaching the new model.
2. We need to manually fetch the target model, because Bookshelf has a weird behaviour (which is known as a bug, see see https://github.com/tgriesser/bookshelf/issues/629). The most important property when attaching a model is `parentFk`, which is the foreign key. This can be null when fetching the model with the option `withRelated`. To ensure quality and consistency, the custom attach wrapper always fetches the target model manual. By fetching the target model (again) is a little performance decrease, but it also has advantages: we can register the event, and directly unregister the event again. So very clean code.
Important: please only use the custom attach wrapper in the future.
* 🎨 token model had overriden the onCreating function because of the created_at field
- we need to ensure that the base onCreating hook get's triggered for ALL models
- if not, they don't get an ObjectId assigned
- in this case: be smart and check if the target model has a created_at field
* 🎨 we don't have a uuid field anymore, remove the usages
- no default uuid creation in models
- i am pretty sure we have some more definitions in our tests (for example in the export json files), but that is too much work to delete them all
* 🎨 do not parse ID to Number
- we had various occurances of parsing all ID's to numbers
- we don't need this behaviour anymore
- ID is string
- i will adapt the ID validation in the next commit
* 🎨 change ID regex for validation
- we only allow: ID as ObjectId, ID as 1 and ID as me
- we need to keep ID 1, because our whole software relies on ID 1 (permissions etc)
* 🎨 owner fixture
- roles: [4] does not work anymore
- 4 means -> static id 4
- this worked in an auto increment system (not even in a system with distributed writes)
- with ObjectId we generate each ID automatically (for static and dynamic resources)
- it is possible to define all id's for static resources still, but that means we need to know which ID is already used and for consistency we have to define ObjectId's for these static resources
- so no static id's anymore, except of: id 1 for owner and id 0 for external usage (because this is required from our permission system)
- NOTE: please read through the comment in the user model
* 🎨 tests: DataGenerator and test utils
First of all: we need to ensure using ObjectId's in the tests. When don't, we can't ensure that ObjectId's work properly.
This commit brings lot's of dynamic into all the static defined id's.
In one of the next commits, i will adapt all the tests.
* 🚨 remove counter in Notification API
- no need to add a counter
- we simply generate ObjectId's (they are auto incremental as well)
- our id validator does only allow ObjectId as id,1 and me
* 🎨 extend contextUser in Base Model
- remove isNumber check, because id's are no longer numbers, except of id 0/1
- use existing isExternalUser
- support id 0/1 as string or number
* ✨ Ghost Owner has id 1
- ensure we define this id in the fixtures.json
- doesn't matter if number or string
* 🎨 functional tests adaptions
- use dynamic id's
* 🎨 fix unit tests
* 🎨 integration tests adaptions
* 🎨 change importer utils
- all our export examples (test/fixtures/exports) contain id's as numbers
- fact: but we ignore them anyway when inserting into the database, see https://github.com/TryGhost/Ghost/blob/master/core/server/data/import/utils.js#L249
- in https://github.com/TryGhost/Ghost/pull/7495/commits/0e6ed957cd54dc02a25cf6fb1ab7d7e723295e2c#diff-70f514a06347c048648be464819503c4L67 i removed parsing id's to integers
- i realised that this ^ check just existed, because the userIdToMap was an object key and object keys are always strings!
- i think this logic is a little bit complicated, but i don't want to refactor this now
- this commit ensures when trying to find the user, the id comparison works again
- i've added more documentation to understand this logic ;)
- plus i renamed an attribute to improve readability
* 🎨 Data-Generator: add more defaults to createUser
- if i use the function DataGenerator.forKnex.createUser i would like to get a full set of defaults
* 🎨 test utils: change/extend function set for functional tests
- functional tests work a bit different
- they boot Ghost and seed the database
- some functional tests have mis-used the test setup
- the test setup needs two sections: integration/unit and functional tests
- any functional test is allowed to either add more data or change data in the existing Ghost db
- but what it should not do is: add test fixtures like roles or users from our DataGenerator and cross fingers it will work
- this commit adds a clean method for functional tests to add extra users
* 🎨 functional tests adaptions
- use last commit to insert users for functional tests clean
- tidy up usage of testUtils.setup or testUtils.doAuth
* 🐛 test utils: reset database before init
- ensure we don't have any left data from other tests in the database when starting ghost
* 🐛 fix test (unrelated to this PR)
- fixes a random failure
- return statement was missing
* 🎨 make changes for invites
2016-11-17 12:09:11 +03:00
throw new Error ( 'setup todo does not exist - spell mistake?' ) ;
2016-05-25 10:34:46 +03:00
}
2014-07-21 21:50:04 +04:00
fixtureOps . push ( toDoList [ toDo ] ) ;
2013-12-29 00:13:47 +04:00
}
2014-07-21 21:50:04 +04:00
} ) ;
return fixtureOps ;
} ;
// ## Test Setup and Teardown
2014-08-09 23:16:54 +04:00
initFixtures = function initFixtures ( ) {
2014-09-10 08:06:24 +04:00
var options = _ . merge ( { init : true } , _ . transform ( arguments , function ( result , val ) {
result [ val ] = true ;
} ) ) ,
2014-08-09 23:16:54 +04:00
fixtureOps = getFixtureOps ( options ) ;
return sequence ( fixtureOps ) ;
} ;
2014-07-21 21:50:04 +04:00
/ * *
* # # Setup Integration Tests
* Setup takes a list of arguments like : 'default' , 'tag' , 'perms:tag' , 'perms:init'
* Setup does 'init' ( DB ) by default
* @ returns { Function }
* /
setup = function setup ( ) {
2014-08-09 23:16:54 +04:00
var self = this ,
args = arguments ;
2014-07-21 21:50:04 +04:00
2016-05-25 10:34:46 +03:00
return function setup ( done ) {
2016-07-15 19:22:41 +03:00
models . init ( ) ;
2016-03-02 07:42:01 +03:00
2016-05-25 10:34:46 +03:00
if ( done ) {
2016-05-19 14:49:22 +03:00
initFixtures . apply ( self , args ) . then ( function ( ) {
2016-05-25 10:34:46 +03:00
done ( ) ;
} ) . catch ( done ) ;
} else {
return initFixtures . apply ( self , args ) ;
}
2014-07-21 21:50:04 +04:00
} ;
} ;
✨ replace auto increment id's by object id (#7495)
* 🛠 bookshelf tarball, bson-objectid
* 🎨 schema changes
- change increment type to string
- add a default fallback for string length 191 (to avoid adding this logic to every single column which uses an ID)
- remove uuid, because ID now represents a global resource identifier
- keep uuid for post, because we are using this as preview id
- keep uuid for clients for now - we are using this param for Ghost-Auth
* ✨ base model: generate ObjectId on creating event
- each new resource get's a auto generate ObjectId
- this logic won't work for attached models, this commit comes later
* 🎨 centralised attach method
When attaching models there are two things important two know
1. To be able to attach an ObjectId, we need to register the `onCreating` event the fetched model!This is caused by the Bookshelf design in general. On this target model we are attaching the new model.
2. We need to manually fetch the target model, because Bookshelf has a weird behaviour (which is known as a bug, see see https://github.com/tgriesser/bookshelf/issues/629). The most important property when attaching a model is `parentFk`, which is the foreign key. This can be null when fetching the model with the option `withRelated`. To ensure quality and consistency, the custom attach wrapper always fetches the target model manual. By fetching the target model (again) is a little performance decrease, but it also has advantages: we can register the event, and directly unregister the event again. So very clean code.
Important: please only use the custom attach wrapper in the future.
* 🎨 token model had overriden the onCreating function because of the created_at field
- we need to ensure that the base onCreating hook get's triggered for ALL models
- if not, they don't get an ObjectId assigned
- in this case: be smart and check if the target model has a created_at field
* 🎨 we don't have a uuid field anymore, remove the usages
- no default uuid creation in models
- i am pretty sure we have some more definitions in our tests (for example in the export json files), but that is too much work to delete them all
* 🎨 do not parse ID to Number
- we had various occurances of parsing all ID's to numbers
- we don't need this behaviour anymore
- ID is string
- i will adapt the ID validation in the next commit
* 🎨 change ID regex for validation
- we only allow: ID as ObjectId, ID as 1 and ID as me
- we need to keep ID 1, because our whole software relies on ID 1 (permissions etc)
* 🎨 owner fixture
- roles: [4] does not work anymore
- 4 means -> static id 4
- this worked in an auto increment system (not even in a system with distributed writes)
- with ObjectId we generate each ID automatically (for static and dynamic resources)
- it is possible to define all id's for static resources still, but that means we need to know which ID is already used and for consistency we have to define ObjectId's for these static resources
- so no static id's anymore, except of: id 1 for owner and id 0 for external usage (because this is required from our permission system)
- NOTE: please read through the comment in the user model
* 🎨 tests: DataGenerator and test utils
First of all: we need to ensure using ObjectId's in the tests. When don't, we can't ensure that ObjectId's work properly.
This commit brings lot's of dynamic into all the static defined id's.
In one of the next commits, i will adapt all the tests.
* 🚨 remove counter in Notification API
- no need to add a counter
- we simply generate ObjectId's (they are auto incremental as well)
- our id validator does only allow ObjectId as id,1 and me
* 🎨 extend contextUser in Base Model
- remove isNumber check, because id's are no longer numbers, except of id 0/1
- use existing isExternalUser
- support id 0/1 as string or number
* ✨ Ghost Owner has id 1
- ensure we define this id in the fixtures.json
- doesn't matter if number or string
* 🎨 functional tests adaptions
- use dynamic id's
* 🎨 fix unit tests
* 🎨 integration tests adaptions
* 🎨 change importer utils
- all our export examples (test/fixtures/exports) contain id's as numbers
- fact: but we ignore them anyway when inserting into the database, see https://github.com/TryGhost/Ghost/blob/master/core/server/data/import/utils.js#L249
- in https://github.com/TryGhost/Ghost/pull/7495/commits/0e6ed957cd54dc02a25cf6fb1ab7d7e723295e2c#diff-70f514a06347c048648be464819503c4L67 i removed parsing id's to integers
- i realised that this ^ check just existed, because the userIdToMap was an object key and object keys are always strings!
- i think this logic is a little bit complicated, but i don't want to refactor this now
- this commit ensures when trying to find the user, the id comparison works again
- i've added more documentation to understand this logic ;)
- plus i renamed an attribute to improve readability
* 🎨 Data-Generator: add more defaults to createUser
- if i use the function DataGenerator.forKnex.createUser i would like to get a full set of defaults
* 🎨 test utils: change/extend function set for functional tests
- functional tests work a bit different
- they boot Ghost and seed the database
- some functional tests have mis-used the test setup
- the test setup needs two sections: integration/unit and functional tests
- any functional test is allowed to either add more data or change data in the existing Ghost db
- but what it should not do is: add test fixtures like roles or users from our DataGenerator and cross fingers it will work
- this commit adds a clean method for functional tests to add extra users
* 🎨 functional tests adaptions
- use last commit to insert users for functional tests clean
- tidy up usage of testUtils.setup or testUtils.doAuth
* 🐛 test utils: reset database before init
- ensure we don't have any left data from other tests in the database when starting ghost
* 🐛 fix test (unrelated to this PR)
- fixes a random failure
- return statement was missing
* 🎨 make changes for invites
2016-11-17 12:09:11 +03:00
// ## Functions for Route Tests (!!)
2014-07-21 21:50:04 +04:00
/ * *
2014-08-09 23:16:54 +04:00
* This function manages the work of ensuring we have an overridden owner user , and grabbing an access token
2014-07-21 21:50:04 +04:00
* @ returns { deferred . promise < AccessToken > }
* /
// TODO make this do the DB init as well
doAuth = function doAuth ( ) {
var options = arguments ,
request = arguments [ 0 ] ,
fixtureOps ;
// Remove request from this list
delete options [ 0 ] ;
2016-08-19 09:32:01 +03:00
2014-07-21 21:50:04 +04:00
// No DB setup, but override the owner
options = _ . merge ( { 'owner:post' : true } , _ . transform ( options , function ( result , val ) {
2015-07-18 03:26:15 +03:00
if ( val ) {
result [ val ] = true ;
}
2014-09-10 08:06:24 +04:00
} ) ) ;
2014-07-21 21:50:04 +04:00
fixtureOps = getFixtureOps ( options ) ;
2014-10-19 23:10:13 +04:00
return sequence ( fixtureOps ) . then ( function ( ) {
return login ( request ) ;
} ) ;
} ;
✨ replace auto increment id's by object id (#7495)
* 🛠 bookshelf tarball, bson-objectid
* 🎨 schema changes
- change increment type to string
- add a default fallback for string length 191 (to avoid adding this logic to every single column which uses an ID)
- remove uuid, because ID now represents a global resource identifier
- keep uuid for post, because we are using this as preview id
- keep uuid for clients for now - we are using this param for Ghost-Auth
* ✨ base model: generate ObjectId on creating event
- each new resource get's a auto generate ObjectId
- this logic won't work for attached models, this commit comes later
* 🎨 centralised attach method
When attaching models there are two things important two know
1. To be able to attach an ObjectId, we need to register the `onCreating` event the fetched model!This is caused by the Bookshelf design in general. On this target model we are attaching the new model.
2. We need to manually fetch the target model, because Bookshelf has a weird behaviour (which is known as a bug, see see https://github.com/tgriesser/bookshelf/issues/629). The most important property when attaching a model is `parentFk`, which is the foreign key. This can be null when fetching the model with the option `withRelated`. To ensure quality and consistency, the custom attach wrapper always fetches the target model manual. By fetching the target model (again) is a little performance decrease, but it also has advantages: we can register the event, and directly unregister the event again. So very clean code.
Important: please only use the custom attach wrapper in the future.
* 🎨 token model had overriden the onCreating function because of the created_at field
- we need to ensure that the base onCreating hook get's triggered for ALL models
- if not, they don't get an ObjectId assigned
- in this case: be smart and check if the target model has a created_at field
* 🎨 we don't have a uuid field anymore, remove the usages
- no default uuid creation in models
- i am pretty sure we have some more definitions in our tests (for example in the export json files), but that is too much work to delete them all
* 🎨 do not parse ID to Number
- we had various occurances of parsing all ID's to numbers
- we don't need this behaviour anymore
- ID is string
- i will adapt the ID validation in the next commit
* 🎨 change ID regex for validation
- we only allow: ID as ObjectId, ID as 1 and ID as me
- we need to keep ID 1, because our whole software relies on ID 1 (permissions etc)
* 🎨 owner fixture
- roles: [4] does not work anymore
- 4 means -> static id 4
- this worked in an auto increment system (not even in a system with distributed writes)
- with ObjectId we generate each ID automatically (for static and dynamic resources)
- it is possible to define all id's for static resources still, but that means we need to know which ID is already used and for consistency we have to define ObjectId's for these static resources
- so no static id's anymore, except of: id 1 for owner and id 0 for external usage (because this is required from our permission system)
- NOTE: please read through the comment in the user model
* 🎨 tests: DataGenerator and test utils
First of all: we need to ensure using ObjectId's in the tests. When don't, we can't ensure that ObjectId's work properly.
This commit brings lot's of dynamic into all the static defined id's.
In one of the next commits, i will adapt all the tests.
* 🚨 remove counter in Notification API
- no need to add a counter
- we simply generate ObjectId's (they are auto incremental as well)
- our id validator does only allow ObjectId as id,1 and me
* 🎨 extend contextUser in Base Model
- remove isNumber check, because id's are no longer numbers, except of id 0/1
- use existing isExternalUser
- support id 0/1 as string or number
* ✨ Ghost Owner has id 1
- ensure we define this id in the fixtures.json
- doesn't matter if number or string
* 🎨 functional tests adaptions
- use dynamic id's
* 🎨 fix unit tests
* 🎨 integration tests adaptions
* 🎨 change importer utils
- all our export examples (test/fixtures/exports) contain id's as numbers
- fact: but we ignore them anyway when inserting into the database, see https://github.com/TryGhost/Ghost/blob/master/core/server/data/import/utils.js#L249
- in https://github.com/TryGhost/Ghost/pull/7495/commits/0e6ed957cd54dc02a25cf6fb1ab7d7e723295e2c#diff-70f514a06347c048648be464819503c4L67 i removed parsing id's to integers
- i realised that this ^ check just existed, because the userIdToMap was an object key and object keys are always strings!
- i think this logic is a little bit complicated, but i don't want to refactor this now
- this commit ensures when trying to find the user, the id comparison works again
- i've added more documentation to understand this logic ;)
- plus i renamed an attribute to improve readability
* 🎨 Data-Generator: add more defaults to createUser
- if i use the function DataGenerator.forKnex.createUser i would like to get a full set of defaults
* 🎨 test utils: change/extend function set for functional tests
- functional tests work a bit different
- they boot Ghost and seed the database
- some functional tests have mis-used the test setup
- the test setup needs two sections: integration/unit and functional tests
- any functional test is allowed to either add more data or change data in the existing Ghost db
- but what it should not do is: add test fixtures like roles or users from our DataGenerator and cross fingers it will work
- this commit adds a clean method for functional tests to add extra users
* 🎨 functional tests adaptions
- use last commit to insert users for functional tests clean
- tidy up usage of testUtils.setup or testUtils.doAuth
* 🐛 test utils: reset database before init
- ensure we don't have any left data from other tests in the database when starting ghost
* 🐛 fix test (unrelated to this PR)
- fixes a random failure
- return statement was missing
* 🎨 make changes for invites
2016-11-17 12:09:11 +03:00
createUser = function createUser ( options ) {
var user = options . user ,
role = options . role ;
return db . knex ( 'users' ) . insert ( user )
. then ( function ( ) {
return db . knex ( 'roles' ) ;
} )
. then ( function ( roles ) {
return db . knex ( 'roles_users' ) . insert ( {
id : ObjectId . generate ( ) ,
role _id : _ . find ( roles , { name : role . name } ) . id ,
user _id : user . id
} ) ;
} )
. then ( function ( ) {
return user ;
} ) ;
} ;
2017-09-26 17:50:26 +03:00
createPost = function createPost ( options ) {
var post = DataGenerator . forKnex . createPost ( options . post ) ;
if ( options . author ) {
post . author _id = options . author . id ;
}
return db . knex ( 'posts' )
. insert ( post )
. then ( function ( ) {
return post ;
} ) ;
} ;
2014-10-19 23:10:13 +04:00
login = function login ( request ) {
✨ replace auto increment id's by object id (#7495)
* 🛠 bookshelf tarball, bson-objectid
* 🎨 schema changes
- change increment type to string
- add a default fallback for string length 191 (to avoid adding this logic to every single column which uses an ID)
- remove uuid, because ID now represents a global resource identifier
- keep uuid for post, because we are using this as preview id
- keep uuid for clients for now - we are using this param for Ghost-Auth
* ✨ base model: generate ObjectId on creating event
- each new resource get's a auto generate ObjectId
- this logic won't work for attached models, this commit comes later
* 🎨 centralised attach method
When attaching models there are two things important two know
1. To be able to attach an ObjectId, we need to register the `onCreating` event the fetched model!This is caused by the Bookshelf design in general. On this target model we are attaching the new model.
2. We need to manually fetch the target model, because Bookshelf has a weird behaviour (which is known as a bug, see see https://github.com/tgriesser/bookshelf/issues/629). The most important property when attaching a model is `parentFk`, which is the foreign key. This can be null when fetching the model with the option `withRelated`. To ensure quality and consistency, the custom attach wrapper always fetches the target model manual. By fetching the target model (again) is a little performance decrease, but it also has advantages: we can register the event, and directly unregister the event again. So very clean code.
Important: please only use the custom attach wrapper in the future.
* 🎨 token model had overriden the onCreating function because of the created_at field
- we need to ensure that the base onCreating hook get's triggered for ALL models
- if not, they don't get an ObjectId assigned
- in this case: be smart and check if the target model has a created_at field
* 🎨 we don't have a uuid field anymore, remove the usages
- no default uuid creation in models
- i am pretty sure we have some more definitions in our tests (for example in the export json files), but that is too much work to delete them all
* 🎨 do not parse ID to Number
- we had various occurances of parsing all ID's to numbers
- we don't need this behaviour anymore
- ID is string
- i will adapt the ID validation in the next commit
* 🎨 change ID regex for validation
- we only allow: ID as ObjectId, ID as 1 and ID as me
- we need to keep ID 1, because our whole software relies on ID 1 (permissions etc)
* 🎨 owner fixture
- roles: [4] does not work anymore
- 4 means -> static id 4
- this worked in an auto increment system (not even in a system with distributed writes)
- with ObjectId we generate each ID automatically (for static and dynamic resources)
- it is possible to define all id's for static resources still, but that means we need to know which ID is already used and for consistency we have to define ObjectId's for these static resources
- so no static id's anymore, except of: id 1 for owner and id 0 for external usage (because this is required from our permission system)
- NOTE: please read through the comment in the user model
* 🎨 tests: DataGenerator and test utils
First of all: we need to ensure using ObjectId's in the tests. When don't, we can't ensure that ObjectId's work properly.
This commit brings lot's of dynamic into all the static defined id's.
In one of the next commits, i will adapt all the tests.
* 🚨 remove counter in Notification API
- no need to add a counter
- we simply generate ObjectId's (they are auto incremental as well)
- our id validator does only allow ObjectId as id,1 and me
* 🎨 extend contextUser in Base Model
- remove isNumber check, because id's are no longer numbers, except of id 0/1
- use existing isExternalUser
- support id 0/1 as string or number
* ✨ Ghost Owner has id 1
- ensure we define this id in the fixtures.json
- doesn't matter if number or string
* 🎨 functional tests adaptions
- use dynamic id's
* 🎨 fix unit tests
* 🎨 integration tests adaptions
* 🎨 change importer utils
- all our export examples (test/fixtures/exports) contain id's as numbers
- fact: but we ignore them anyway when inserting into the database, see https://github.com/TryGhost/Ghost/blob/master/core/server/data/import/utils.js#L249
- in https://github.com/TryGhost/Ghost/pull/7495/commits/0e6ed957cd54dc02a25cf6fb1ab7d7e723295e2c#diff-70f514a06347c048648be464819503c4L67 i removed parsing id's to integers
- i realised that this ^ check just existed, because the userIdToMap was an object key and object keys are always strings!
- i think this logic is a little bit complicated, but i don't want to refactor this now
- this commit ensures when trying to find the user, the id comparison works again
- i've added more documentation to understand this logic ;)
- plus i renamed an attribute to improve readability
* 🎨 Data-Generator: add more defaults to createUser
- if i use the function DataGenerator.forKnex.createUser i would like to get a full set of defaults
* 🎨 test utils: change/extend function set for functional tests
- functional tests work a bit different
- they boot Ghost and seed the database
- some functional tests have mis-used the test setup
- the test setup needs two sections: integration/unit and functional tests
- any functional test is allowed to either add more data or change data in the existing Ghost db
- but what it should not do is: add test fixtures like roles or users from our DataGenerator and cross fingers it will work
- this commit adds a clean method for functional tests to add extra users
* 🎨 functional tests adaptions
- use last commit to insert users for functional tests clean
- tidy up usage of testUtils.setup or testUtils.doAuth
* 🐛 test utils: reset database before init
- ensure we don't have any left data from other tests in the database when starting ghost
* 🐛 fix test (unrelated to this PR)
- fixes a random failure
- return statement was missing
* 🎨 make changes for invites
2016-11-17 12:09:11 +03:00
// CASE: by default we use the owner to login
if ( ! request . user ) {
request . user = DataGenerator . Content . users [ 0 ] ;
}
2014-10-19 23:10:13 +04:00
return new Promise ( function ( resolve , reject ) {
request . post ( '/ghost/api/v0.1/authentication/token/' )
2016-09-13 18:41:14 +03:00
. set ( 'Origin' , config . get ( 'url' ) )
2015-10-22 16:28:47 +03:00
. send ( {
grant _type : 'password' ,
✨ replace auto increment id's by object id (#7495)
* 🛠 bookshelf tarball, bson-objectid
* 🎨 schema changes
- change increment type to string
- add a default fallback for string length 191 (to avoid adding this logic to every single column which uses an ID)
- remove uuid, because ID now represents a global resource identifier
- keep uuid for post, because we are using this as preview id
- keep uuid for clients for now - we are using this param for Ghost-Auth
* ✨ base model: generate ObjectId on creating event
- each new resource get's a auto generate ObjectId
- this logic won't work for attached models, this commit comes later
* 🎨 centralised attach method
When attaching models there are two things important two know
1. To be able to attach an ObjectId, we need to register the `onCreating` event the fetched model!This is caused by the Bookshelf design in general. On this target model we are attaching the new model.
2. We need to manually fetch the target model, because Bookshelf has a weird behaviour (which is known as a bug, see see https://github.com/tgriesser/bookshelf/issues/629). The most important property when attaching a model is `parentFk`, which is the foreign key. This can be null when fetching the model with the option `withRelated`. To ensure quality and consistency, the custom attach wrapper always fetches the target model manual. By fetching the target model (again) is a little performance decrease, but it also has advantages: we can register the event, and directly unregister the event again. So very clean code.
Important: please only use the custom attach wrapper in the future.
* 🎨 token model had overriden the onCreating function because of the created_at field
- we need to ensure that the base onCreating hook get's triggered for ALL models
- if not, they don't get an ObjectId assigned
- in this case: be smart and check if the target model has a created_at field
* 🎨 we don't have a uuid field anymore, remove the usages
- no default uuid creation in models
- i am pretty sure we have some more definitions in our tests (for example in the export json files), but that is too much work to delete them all
* 🎨 do not parse ID to Number
- we had various occurances of parsing all ID's to numbers
- we don't need this behaviour anymore
- ID is string
- i will adapt the ID validation in the next commit
* 🎨 change ID regex for validation
- we only allow: ID as ObjectId, ID as 1 and ID as me
- we need to keep ID 1, because our whole software relies on ID 1 (permissions etc)
* 🎨 owner fixture
- roles: [4] does not work anymore
- 4 means -> static id 4
- this worked in an auto increment system (not even in a system with distributed writes)
- with ObjectId we generate each ID automatically (for static and dynamic resources)
- it is possible to define all id's for static resources still, but that means we need to know which ID is already used and for consistency we have to define ObjectId's for these static resources
- so no static id's anymore, except of: id 1 for owner and id 0 for external usage (because this is required from our permission system)
- NOTE: please read through the comment in the user model
* 🎨 tests: DataGenerator and test utils
First of all: we need to ensure using ObjectId's in the tests. When don't, we can't ensure that ObjectId's work properly.
This commit brings lot's of dynamic into all the static defined id's.
In one of the next commits, i will adapt all the tests.
* 🚨 remove counter in Notification API
- no need to add a counter
- we simply generate ObjectId's (they are auto incremental as well)
- our id validator does only allow ObjectId as id,1 and me
* 🎨 extend contextUser in Base Model
- remove isNumber check, because id's are no longer numbers, except of id 0/1
- use existing isExternalUser
- support id 0/1 as string or number
* ✨ Ghost Owner has id 1
- ensure we define this id in the fixtures.json
- doesn't matter if number or string
* 🎨 functional tests adaptions
- use dynamic id's
* 🎨 fix unit tests
* 🎨 integration tests adaptions
* 🎨 change importer utils
- all our export examples (test/fixtures/exports) contain id's as numbers
- fact: but we ignore them anyway when inserting into the database, see https://github.com/TryGhost/Ghost/blob/master/core/server/data/import/utils.js#L249
- in https://github.com/TryGhost/Ghost/pull/7495/commits/0e6ed957cd54dc02a25cf6fb1ab7d7e723295e2c#diff-70f514a06347c048648be464819503c4L67 i removed parsing id's to integers
- i realised that this ^ check just existed, because the userIdToMap was an object key and object keys are always strings!
- i think this logic is a little bit complicated, but i don't want to refactor this now
- this commit ensures when trying to find the user, the id comparison works again
- i've added more documentation to understand this logic ;)
- plus i renamed an attribute to improve readability
* 🎨 Data-Generator: add more defaults to createUser
- if i use the function DataGenerator.forKnex.createUser i would like to get a full set of defaults
* 🎨 test utils: change/extend function set for functional tests
- functional tests work a bit different
- they boot Ghost and seed the database
- some functional tests have mis-used the test setup
- the test setup needs two sections: integration/unit and functional tests
- any functional test is allowed to either add more data or change data in the existing Ghost db
- but what it should not do is: add test fixtures like roles or users from our DataGenerator and cross fingers it will work
- this commit adds a clean method for functional tests to add extra users
* 🎨 functional tests adaptions
- use last commit to insert users for functional tests clean
- tidy up usage of testUtils.setup or testUtils.doAuth
* 🐛 test utils: reset database before init
- ensure we don't have any left data from other tests in the database when starting ghost
* 🐛 fix test (unrelated to this PR)
- fixes a random failure
- return statement was missing
* 🎨 make changes for invites
2016-11-17 12:09:11 +03:00
username : request . user . email ,
2017-10-18 19:45:41 +03:00
password : 'Sl1m3rson99' ,
2015-10-22 16:28:47 +03:00
client _id : 'ghost-admin' ,
client _secret : 'not_available'
2016-02-07 22:10:27 +03:00
} ) . then ( function then ( res ) {
2017-03-09 22:38:20 +03:00
if ( res . statusCode !== 200 ) {
return reject ( new errors . GhostError ( {
message : res . body . errors [ 0 ] . message
} ) ) ;
}
✨ replace auto increment id's by object id (#7495)
* 🛠 bookshelf tarball, bson-objectid
* 🎨 schema changes
- change increment type to string
- add a default fallback for string length 191 (to avoid adding this logic to every single column which uses an ID)
- remove uuid, because ID now represents a global resource identifier
- keep uuid for post, because we are using this as preview id
- keep uuid for clients for now - we are using this param for Ghost-Auth
* ✨ base model: generate ObjectId on creating event
- each new resource get's a auto generate ObjectId
- this logic won't work for attached models, this commit comes later
* 🎨 centralised attach method
When attaching models there are two things important two know
1. To be able to attach an ObjectId, we need to register the `onCreating` event the fetched model!This is caused by the Bookshelf design in general. On this target model we are attaching the new model.
2. We need to manually fetch the target model, because Bookshelf has a weird behaviour (which is known as a bug, see see https://github.com/tgriesser/bookshelf/issues/629). The most important property when attaching a model is `parentFk`, which is the foreign key. This can be null when fetching the model with the option `withRelated`. To ensure quality and consistency, the custom attach wrapper always fetches the target model manual. By fetching the target model (again) is a little performance decrease, but it also has advantages: we can register the event, and directly unregister the event again. So very clean code.
Important: please only use the custom attach wrapper in the future.
* 🎨 token model had overriden the onCreating function because of the created_at field
- we need to ensure that the base onCreating hook get's triggered for ALL models
- if not, they don't get an ObjectId assigned
- in this case: be smart and check if the target model has a created_at field
* 🎨 we don't have a uuid field anymore, remove the usages
- no default uuid creation in models
- i am pretty sure we have some more definitions in our tests (for example in the export json files), but that is too much work to delete them all
* 🎨 do not parse ID to Number
- we had various occurances of parsing all ID's to numbers
- we don't need this behaviour anymore
- ID is string
- i will adapt the ID validation in the next commit
* 🎨 change ID regex for validation
- we only allow: ID as ObjectId, ID as 1 and ID as me
- we need to keep ID 1, because our whole software relies on ID 1 (permissions etc)
* 🎨 owner fixture
- roles: [4] does not work anymore
- 4 means -> static id 4
- this worked in an auto increment system (not even in a system with distributed writes)
- with ObjectId we generate each ID automatically (for static and dynamic resources)
- it is possible to define all id's for static resources still, but that means we need to know which ID is already used and for consistency we have to define ObjectId's for these static resources
- so no static id's anymore, except of: id 1 for owner and id 0 for external usage (because this is required from our permission system)
- NOTE: please read through the comment in the user model
* 🎨 tests: DataGenerator and test utils
First of all: we need to ensure using ObjectId's in the tests. When don't, we can't ensure that ObjectId's work properly.
This commit brings lot's of dynamic into all the static defined id's.
In one of the next commits, i will adapt all the tests.
* 🚨 remove counter in Notification API
- no need to add a counter
- we simply generate ObjectId's (they are auto incremental as well)
- our id validator does only allow ObjectId as id,1 and me
* 🎨 extend contextUser in Base Model
- remove isNumber check, because id's are no longer numbers, except of id 0/1
- use existing isExternalUser
- support id 0/1 as string or number
* ✨ Ghost Owner has id 1
- ensure we define this id in the fixtures.json
- doesn't matter if number or string
* 🎨 functional tests adaptions
- use dynamic id's
* 🎨 fix unit tests
* 🎨 integration tests adaptions
* 🎨 change importer utils
- all our export examples (test/fixtures/exports) contain id's as numbers
- fact: but we ignore them anyway when inserting into the database, see https://github.com/TryGhost/Ghost/blob/master/core/server/data/import/utils.js#L249
- in https://github.com/TryGhost/Ghost/pull/7495/commits/0e6ed957cd54dc02a25cf6fb1ab7d7e723295e2c#diff-70f514a06347c048648be464819503c4L67 i removed parsing id's to integers
- i realised that this ^ check just existed, because the userIdToMap was an object key and object keys are always strings!
- i think this logic is a little bit complicated, but i don't want to refactor this now
- this commit ensures when trying to find the user, the id comparison works again
- i've added more documentation to understand this logic ;)
- plus i renamed an attribute to improve readability
* 🎨 Data-Generator: add more defaults to createUser
- if i use the function DataGenerator.forKnex.createUser i would like to get a full set of defaults
* 🎨 test utils: change/extend function set for functional tests
- functional tests work a bit different
- they boot Ghost and seed the database
- some functional tests have mis-used the test setup
- the test setup needs two sections: integration/unit and functional tests
- any functional test is allowed to either add more data or change data in the existing Ghost db
- but what it should not do is: add test fixtures like roles or users from our DataGenerator and cross fingers it will work
- this commit adds a clean method for functional tests to add extra users
* 🎨 functional tests adaptions
- use last commit to insert users for functional tests clean
- tidy up usage of testUtils.setup or testUtils.doAuth
* 🐛 test utils: reset database before init
- ensure we don't have any left data from other tests in the database when starting ghost
* 🐛 fix test (unrelated to this PR)
- fixes a random failure
- return statement was missing
* 🎨 make changes for invites
2016-11-17 12:09:11 +03:00
2017-03-09 22:38:20 +03:00
resolve ( res . body . access _token ) ;
} , reject ) ;
2014-10-19 23:10:13 +04:00
} ) ;
} ;
togglePermalinks = function togglePermalinks ( request , toggle ) {
var permalinkString = toggle === 'date' ? '/:year/:month/:day/:slug/' : '/:slug/' ;
2014-08-17 10:17:23 +04:00
return new Promise ( function ( resolve , reject ) {
2014-10-19 23:10:13 +04:00
doAuth ( request ) . then ( function ( token ) {
request . put ( '/ghost/api/v0.1/settings/' )
. set ( 'Authorization' , 'Bearer ' + token )
2017-03-09 22:38:20 +03:00
. send ( {
settings : [
{
uuid : '75e994ae-490e-45e6-9207-0eab409c1c04' ,
key : 'permalinks' ,
value : permalinkString ,
type : 'blog' ,
created _at : '2014-10-16T17:39:16.005Z' ,
created _by : 1 ,
updated _at : '2014-10-20T19:44:18.077Z' ,
updated _by : 1
}
]
} )
2014-08-17 10:17:23 +04:00
. end ( function ( err , res ) {
if ( err ) {
return reject ( err ) ;
}
2016-09-13 19:23:02 +03:00
if ( res . statusCode !== 200 ) {
return reject ( res . body ) ;
}
2014-10-19 23:10:13 +04:00
resolve ( res . body ) ;
2014-08-17 10:17:23 +04:00
} ) ;
} ) ;
2013-12-29 00:13:47 +04:00
} ) ;
2014-07-21 21:50:04 +04:00
} ;
2017-03-09 22:38:20 +03:00
/ * *
* Has to run in a transaction for MySQL , otherwise the foreign key check does not work .
* Sqlite3 has no truncate command .
* /
2014-07-21 21:50:04 +04:00
teardown = function teardown ( done ) {
2017-03-09 22:38:20 +03:00
debug ( 'Database teardown' ) ;
var tables = schemaTables . concat ( [ 'migrations' ] ) ;
2016-11-07 14:39:49 +03:00
2017-03-09 22:38:20 +03:00
if ( config . get ( 'database:client' ) === 'sqlite3' ) {
return Promise
. mapSeries ( tables , function createTable ( table ) {
return db . knex . raw ( 'DELETE FROM ' + table + ';' ) ;
} )
2016-11-07 14:39:49 +03:00
. then ( function ( ) {
2017-03-09 22:38:20 +03:00
done && done ( ) ;
2016-11-07 14:39:49 +03:00
} )
2017-03-09 22:38:20 +03:00
. catch ( function ( err ) {
// CASE: table does not exist
if ( err . errno === 1 ) {
return done && done ( ) ;
}
done && done ( err ) ;
} ) ;
2016-05-25 10:34:46 +03:00
}
2017-03-09 22:38:20 +03:00
return db . knex . transaction ( function ( trx ) {
return db . knex . raw ( 'SET FOREIGN_KEY_CHECKS=0;' ) . transacting ( trx )
. then ( function ( ) {
return Promise
. each ( tables , function createTable ( table ) {
return db . knex . raw ( 'TRUNCATE ' + table + ';' ) . transacting ( trx ) ;
} ) ;
} )
. then ( function ( ) {
return db . knex . raw ( 'SET FOREIGN_KEY_CHECKS=1;' ) . transacting ( trx ) ;
} )
. then ( function ( ) {
done && done ( ) ;
} )
. catch ( function ( err ) {
// CASE: table does not exist
if ( err . errno === 1146 ) {
return done && done ( ) ;
}
return done ? done ( err ) : Promise . reject ( err ) ;
} ) ;
} ) ;
2014-07-21 13:29:03 +04:00
} ;
2016-05-19 14:49:22 +03:00
/ * *
* offer helper functions for mocking
* we start with a small function set to mock non existent modules
* /
originalRequireFn = Module . prototype . require ;
mockNotExistingModule = function mockNotExistingModule ( modulePath , module ) {
Module . prototype . require = function ( path ) {
if ( path . match ( modulePath ) ) {
return module ;
}
return originalRequireFn . apply ( this , arguments ) ;
} ;
} ;
unmockNotExistingModule = function unmockNotExistingModule ( ) {
Module . prototype . require = originalRequireFn ;
} ;
2016-10-11 15:37:11 +03:00
/ * *
* 1. sephiroth init db
* 2. start ghost
* /
2017-09-25 20:35:57 +03:00
startGhost = function startGhost ( options ) {
options = options || { redirectsFile : true } ;
2017-09-21 17:05:35 +03:00
var contentFolderForTests = path . join ( os . tmpdir ( ) , uuid . v1 ( ) , 'ghost-test' ) ;
/ * *
* We never use the root content folder for testing !
* We use a tmp folder .
* @ TODO : add testUtils . stopServer and ensure we remove the tmp folder .
* /
configUtils . set ( 'paths:contentPath' , contentFolderForTests ) ;
fs . ensureDirSync ( contentFolderForTests ) ;
fs . ensureDirSync ( path . join ( contentFolderForTests , 'data' ) ) ;
fs . ensureDirSync ( path . join ( contentFolderForTests , 'themes' ) ) ;
fs . ensureDirSync ( path . join ( contentFolderForTests , 'images' ) ) ;
fs . ensureDirSync ( path . join ( contentFolderForTests , 'logs' ) ) ;
fs . ensureDirSync ( path . join ( contentFolderForTests , 'adapters' ) ) ;
// Copy all themes into the new test content folder. Default active theme is always casper. If you want to use a different theme, you have to set the active theme (e.g. stub)
fs . copySync ( path . join ( _ _dirname , 'fixtures' , 'themes' ) , path . join ( contentFolderForTests , 'themes' ) ) ;
2017-09-25 20:35:57 +03:00
if ( options . redirectsFile ) {
fs . copySync ( path . join ( _ _dirname , 'fixtures' , 'data' , 'redirects.json' ) , path . join ( contentFolderForTests , 'data' , 'redirects.json' ) ) ;
}
2017-09-21 17:05:35 +03:00
✨ replace auto increment id's by object id (#7495)
* 🛠 bookshelf tarball, bson-objectid
* 🎨 schema changes
- change increment type to string
- add a default fallback for string length 191 (to avoid adding this logic to every single column which uses an ID)
- remove uuid, because ID now represents a global resource identifier
- keep uuid for post, because we are using this as preview id
- keep uuid for clients for now - we are using this param for Ghost-Auth
* ✨ base model: generate ObjectId on creating event
- each new resource get's a auto generate ObjectId
- this logic won't work for attached models, this commit comes later
* 🎨 centralised attach method
When attaching models there are two things important two know
1. To be able to attach an ObjectId, we need to register the `onCreating` event the fetched model!This is caused by the Bookshelf design in general. On this target model we are attaching the new model.
2. We need to manually fetch the target model, because Bookshelf has a weird behaviour (which is known as a bug, see see https://github.com/tgriesser/bookshelf/issues/629). The most important property when attaching a model is `parentFk`, which is the foreign key. This can be null when fetching the model with the option `withRelated`. To ensure quality and consistency, the custom attach wrapper always fetches the target model manual. By fetching the target model (again) is a little performance decrease, but it also has advantages: we can register the event, and directly unregister the event again. So very clean code.
Important: please only use the custom attach wrapper in the future.
* 🎨 token model had overriden the onCreating function because of the created_at field
- we need to ensure that the base onCreating hook get's triggered for ALL models
- if not, they don't get an ObjectId assigned
- in this case: be smart and check if the target model has a created_at field
* 🎨 we don't have a uuid field anymore, remove the usages
- no default uuid creation in models
- i am pretty sure we have some more definitions in our tests (for example in the export json files), but that is too much work to delete them all
* 🎨 do not parse ID to Number
- we had various occurances of parsing all ID's to numbers
- we don't need this behaviour anymore
- ID is string
- i will adapt the ID validation in the next commit
* 🎨 change ID regex for validation
- we only allow: ID as ObjectId, ID as 1 and ID as me
- we need to keep ID 1, because our whole software relies on ID 1 (permissions etc)
* 🎨 owner fixture
- roles: [4] does not work anymore
- 4 means -> static id 4
- this worked in an auto increment system (not even in a system with distributed writes)
- with ObjectId we generate each ID automatically (for static and dynamic resources)
- it is possible to define all id's for static resources still, but that means we need to know which ID is already used and for consistency we have to define ObjectId's for these static resources
- so no static id's anymore, except of: id 1 for owner and id 0 for external usage (because this is required from our permission system)
- NOTE: please read through the comment in the user model
* 🎨 tests: DataGenerator and test utils
First of all: we need to ensure using ObjectId's in the tests. When don't, we can't ensure that ObjectId's work properly.
This commit brings lot's of dynamic into all the static defined id's.
In one of the next commits, i will adapt all the tests.
* 🚨 remove counter in Notification API
- no need to add a counter
- we simply generate ObjectId's (they are auto incremental as well)
- our id validator does only allow ObjectId as id,1 and me
* 🎨 extend contextUser in Base Model
- remove isNumber check, because id's are no longer numbers, except of id 0/1
- use existing isExternalUser
- support id 0/1 as string or number
* ✨ Ghost Owner has id 1
- ensure we define this id in the fixtures.json
- doesn't matter if number or string
* 🎨 functional tests adaptions
- use dynamic id's
* 🎨 fix unit tests
* 🎨 integration tests adaptions
* 🎨 change importer utils
- all our export examples (test/fixtures/exports) contain id's as numbers
- fact: but we ignore them anyway when inserting into the database, see https://github.com/TryGhost/Ghost/blob/master/core/server/data/import/utils.js#L249
- in https://github.com/TryGhost/Ghost/pull/7495/commits/0e6ed957cd54dc02a25cf6fb1ab7d7e723295e2c#diff-70f514a06347c048648be464819503c4L67 i removed parsing id's to integers
- i realised that this ^ check just existed, because the userIdToMap was an object key and object keys are always strings!
- i think this logic is a little bit complicated, but i don't want to refactor this now
- this commit ensures when trying to find the user, the id comparison works again
- i've added more documentation to understand this logic ;)
- plus i renamed an attribute to improve readability
* 🎨 Data-Generator: add more defaults to createUser
- if i use the function DataGenerator.forKnex.createUser i would like to get a full set of defaults
* 🎨 test utils: change/extend function set for functional tests
- functional tests work a bit different
- they boot Ghost and seed the database
- some functional tests have mis-used the test setup
- the test setup needs two sections: integration/unit and functional tests
- any functional test is allowed to either add more data or change data in the existing Ghost db
- but what it should not do is: add test fixtures like roles or users from our DataGenerator and cross fingers it will work
- this commit adds a clean method for functional tests to add extra users
* 🎨 functional tests adaptions
- use last commit to insert users for functional tests clean
- tidy up usage of testUtils.setup or testUtils.doAuth
* 🐛 test utils: reset database before init
- ensure we don't have any left data from other tests in the database when starting ghost
* 🐛 fix test (unrelated to this PR)
- fixes a random failure
- return statement was missing
* 🎨 make changes for invites
2016-11-17 12:09:11 +03:00
return knexMigrator . reset ( )
. then ( function initialiseDatabase ( ) {
return knexMigrator . init ( ) ;
} )
. then ( function startGhost ( ) {
2016-10-11 15:37:11 +03:00
return ghost ( ) ;
} ) ;
} ;
2013-10-08 05:39:33 +04:00
module . exports = {
2016-10-11 15:37:11 +03:00
startGhost : startGhost ,
2014-07-21 13:29:03 +04:00
teardown : teardown ,
2014-07-21 21:50:04 +04:00
setup : setup ,
doAuth : doAuth ,
✨ replace auto increment id's by object id (#7495)
* 🛠 bookshelf tarball, bson-objectid
* 🎨 schema changes
- change increment type to string
- add a default fallback for string length 191 (to avoid adding this logic to every single column which uses an ID)
- remove uuid, because ID now represents a global resource identifier
- keep uuid for post, because we are using this as preview id
- keep uuid for clients for now - we are using this param for Ghost-Auth
* ✨ base model: generate ObjectId on creating event
- each new resource get's a auto generate ObjectId
- this logic won't work for attached models, this commit comes later
* 🎨 centralised attach method
When attaching models there are two things important two know
1. To be able to attach an ObjectId, we need to register the `onCreating` event the fetched model!This is caused by the Bookshelf design in general. On this target model we are attaching the new model.
2. We need to manually fetch the target model, because Bookshelf has a weird behaviour (which is known as a bug, see see https://github.com/tgriesser/bookshelf/issues/629). The most important property when attaching a model is `parentFk`, which is the foreign key. This can be null when fetching the model with the option `withRelated`. To ensure quality and consistency, the custom attach wrapper always fetches the target model manual. By fetching the target model (again) is a little performance decrease, but it also has advantages: we can register the event, and directly unregister the event again. So very clean code.
Important: please only use the custom attach wrapper in the future.
* 🎨 token model had overriden the onCreating function because of the created_at field
- we need to ensure that the base onCreating hook get's triggered for ALL models
- if not, they don't get an ObjectId assigned
- in this case: be smart and check if the target model has a created_at field
* 🎨 we don't have a uuid field anymore, remove the usages
- no default uuid creation in models
- i am pretty sure we have some more definitions in our tests (for example in the export json files), but that is too much work to delete them all
* 🎨 do not parse ID to Number
- we had various occurances of parsing all ID's to numbers
- we don't need this behaviour anymore
- ID is string
- i will adapt the ID validation in the next commit
* 🎨 change ID regex for validation
- we only allow: ID as ObjectId, ID as 1 and ID as me
- we need to keep ID 1, because our whole software relies on ID 1 (permissions etc)
* 🎨 owner fixture
- roles: [4] does not work anymore
- 4 means -> static id 4
- this worked in an auto increment system (not even in a system with distributed writes)
- with ObjectId we generate each ID automatically (for static and dynamic resources)
- it is possible to define all id's for static resources still, but that means we need to know which ID is already used and for consistency we have to define ObjectId's for these static resources
- so no static id's anymore, except of: id 1 for owner and id 0 for external usage (because this is required from our permission system)
- NOTE: please read through the comment in the user model
* 🎨 tests: DataGenerator and test utils
First of all: we need to ensure using ObjectId's in the tests. When don't, we can't ensure that ObjectId's work properly.
This commit brings lot's of dynamic into all the static defined id's.
In one of the next commits, i will adapt all the tests.
* 🚨 remove counter in Notification API
- no need to add a counter
- we simply generate ObjectId's (they are auto incremental as well)
- our id validator does only allow ObjectId as id,1 and me
* 🎨 extend contextUser in Base Model
- remove isNumber check, because id's are no longer numbers, except of id 0/1
- use existing isExternalUser
- support id 0/1 as string or number
* ✨ Ghost Owner has id 1
- ensure we define this id in the fixtures.json
- doesn't matter if number or string
* 🎨 functional tests adaptions
- use dynamic id's
* 🎨 fix unit tests
* 🎨 integration tests adaptions
* 🎨 change importer utils
- all our export examples (test/fixtures/exports) contain id's as numbers
- fact: but we ignore them anyway when inserting into the database, see https://github.com/TryGhost/Ghost/blob/master/core/server/data/import/utils.js#L249
- in https://github.com/TryGhost/Ghost/pull/7495/commits/0e6ed957cd54dc02a25cf6fb1ab7d7e723295e2c#diff-70f514a06347c048648be464819503c4L67 i removed parsing id's to integers
- i realised that this ^ check just existed, because the userIdToMap was an object key and object keys are always strings!
- i think this logic is a little bit complicated, but i don't want to refactor this now
- this commit ensures when trying to find the user, the id comparison works again
- i've added more documentation to understand this logic ;)
- plus i renamed an attribute to improve readability
* 🎨 Data-Generator: add more defaults to createUser
- if i use the function DataGenerator.forKnex.createUser i would like to get a full set of defaults
* 🎨 test utils: change/extend function set for functional tests
- functional tests work a bit different
- they boot Ghost and seed the database
- some functional tests have mis-used the test setup
- the test setup needs two sections: integration/unit and functional tests
- any functional test is allowed to either add more data or change data in the existing Ghost db
- but what it should not do is: add test fixtures like roles or users from our DataGenerator and cross fingers it will work
- this commit adds a clean method for functional tests to add extra users
* 🎨 functional tests adaptions
- use last commit to insert users for functional tests clean
- tidy up usage of testUtils.setup or testUtils.doAuth
* 🐛 test utils: reset database before init
- ensure we don't have any left data from other tests in the database when starting ghost
* 🐛 fix test (unrelated to this PR)
- fixes a random failure
- return statement was missing
* 🎨 make changes for invites
2016-11-17 12:09:11 +03:00
createUser : createUser ,
2017-09-26 17:50:26 +03:00
createPost : createPost ,
2014-10-19 23:10:13 +04:00
login : login ,
togglePermalinks : togglePermalinks ,
2014-07-21 13:29:03 +04:00
2016-05-19 14:49:22 +03:00
mockNotExistingModule : mockNotExistingModule ,
unmockNotExistingModule : unmockNotExistingModule ,
2017-10-26 13:03:53 +03:00
/ * *
* renderObject : res . render ( view , dbResponse )
* templateOptions : hbs . updateTemplateOptions ( ... )
* /
createHbsResponse : function createHbsResponse ( options ) {
var renderObject = options . renderObject || { } ,
templateOptions = options . templateOptions ,
locals = options . locals || { } ,
hbsStructure = {
data : {
blog : { } ,
config : { } ,
labs : { } ,
root : {
_locals : { }
}
}
} ;
_ . merge ( hbsStructure . data , templateOptions ) ;
_ . merge ( hbsStructure . data . root , renderObject ) ;
_ . merge ( hbsStructure . data . root , locals ) ;
hbsStructure . data . root . _locals = locals ;
return hbsStructure ;
} ,
2014-08-09 23:16:54 +04:00
initFixtures : initFixtures ,
2013-10-08 05:39:33 +04:00
initData : initData ,
clearData : clearData ,
2016-11-08 14:33:19 +03:00
clearBruteData : clearBruteData ,
2014-07-21 21:50:04 +04:00
2016-06-10 08:20:13 +03:00
mocks : mocks ,
2014-07-21 21:50:04 +04:00
fixtures : fixtures ,
2013-12-29 00:13:47 +04:00
2013-10-08 05:39:33 +04:00
DataGenerator : DataGenerator ,
✨ replace auto increment id's by object id (#7495)
* 🛠 bookshelf tarball, bson-objectid
* 🎨 schema changes
- change increment type to string
- add a default fallback for string length 191 (to avoid adding this logic to every single column which uses an ID)
- remove uuid, because ID now represents a global resource identifier
- keep uuid for post, because we are using this as preview id
- keep uuid for clients for now - we are using this param for Ghost-Auth
* ✨ base model: generate ObjectId on creating event
- each new resource get's a auto generate ObjectId
- this logic won't work for attached models, this commit comes later
* 🎨 centralised attach method
When attaching models there are two things important two know
1. To be able to attach an ObjectId, we need to register the `onCreating` event the fetched model!This is caused by the Bookshelf design in general. On this target model we are attaching the new model.
2. We need to manually fetch the target model, because Bookshelf has a weird behaviour (which is known as a bug, see see https://github.com/tgriesser/bookshelf/issues/629). The most important property when attaching a model is `parentFk`, which is the foreign key. This can be null when fetching the model with the option `withRelated`. To ensure quality and consistency, the custom attach wrapper always fetches the target model manual. By fetching the target model (again) is a little performance decrease, but it also has advantages: we can register the event, and directly unregister the event again. So very clean code.
Important: please only use the custom attach wrapper in the future.
* 🎨 token model had overriden the onCreating function because of the created_at field
- we need to ensure that the base onCreating hook get's triggered for ALL models
- if not, they don't get an ObjectId assigned
- in this case: be smart and check if the target model has a created_at field
* 🎨 we don't have a uuid field anymore, remove the usages
- no default uuid creation in models
- i am pretty sure we have some more definitions in our tests (for example in the export json files), but that is too much work to delete them all
* 🎨 do not parse ID to Number
- we had various occurances of parsing all ID's to numbers
- we don't need this behaviour anymore
- ID is string
- i will adapt the ID validation in the next commit
* 🎨 change ID regex for validation
- we only allow: ID as ObjectId, ID as 1 and ID as me
- we need to keep ID 1, because our whole software relies on ID 1 (permissions etc)
* 🎨 owner fixture
- roles: [4] does not work anymore
- 4 means -> static id 4
- this worked in an auto increment system (not even in a system with distributed writes)
- with ObjectId we generate each ID automatically (for static and dynamic resources)
- it is possible to define all id's for static resources still, but that means we need to know which ID is already used and for consistency we have to define ObjectId's for these static resources
- so no static id's anymore, except of: id 1 for owner and id 0 for external usage (because this is required from our permission system)
- NOTE: please read through the comment in the user model
* 🎨 tests: DataGenerator and test utils
First of all: we need to ensure using ObjectId's in the tests. When don't, we can't ensure that ObjectId's work properly.
This commit brings lot's of dynamic into all the static defined id's.
In one of the next commits, i will adapt all the tests.
* 🚨 remove counter in Notification API
- no need to add a counter
- we simply generate ObjectId's (they are auto incremental as well)
- our id validator does only allow ObjectId as id,1 and me
* 🎨 extend contextUser in Base Model
- remove isNumber check, because id's are no longer numbers, except of id 0/1
- use existing isExternalUser
- support id 0/1 as string or number
* ✨ Ghost Owner has id 1
- ensure we define this id in the fixtures.json
- doesn't matter if number or string
* 🎨 functional tests adaptions
- use dynamic id's
* 🎨 fix unit tests
* 🎨 integration tests adaptions
* 🎨 change importer utils
- all our export examples (test/fixtures/exports) contain id's as numbers
- fact: but we ignore them anyway when inserting into the database, see https://github.com/TryGhost/Ghost/blob/master/core/server/data/import/utils.js#L249
- in https://github.com/TryGhost/Ghost/pull/7495/commits/0e6ed957cd54dc02a25cf6fb1ab7d7e723295e2c#diff-70f514a06347c048648be464819503c4L67 i removed parsing id's to integers
- i realised that this ^ check just existed, because the userIdToMap was an object key and object keys are always strings!
- i think this logic is a little bit complicated, but i don't want to refactor this now
- this commit ensures when trying to find the user, the id comparison works again
- i've added more documentation to understand this logic ;)
- plus i renamed an attribute to improve readability
* 🎨 Data-Generator: add more defaults to createUser
- if i use the function DataGenerator.forKnex.createUser i would like to get a full set of defaults
* 🎨 test utils: change/extend function set for functional tests
- functional tests work a bit different
- they boot Ghost and seed the database
- some functional tests have mis-used the test setup
- the test setup needs two sections: integration/unit and functional tests
- any functional test is allowed to either add more data or change data in the existing Ghost db
- but what it should not do is: add test fixtures like roles or users from our DataGenerator and cross fingers it will work
- this commit adds a clean method for functional tests to add extra users
* 🎨 functional tests adaptions
- use last commit to insert users for functional tests clean
- tidy up usage of testUtils.setup or testUtils.doAuth
* 🐛 test utils: reset database before init
- ensure we don't have any left data from other tests in the database when starting ghost
* 🐛 fix test (unrelated to this PR)
- fixes a random failure
- return statement was missing
* 🎨 make changes for invites
2016-11-17 12:09:11 +03:00
filterData : filterData ,
2014-02-22 05:25:31 +04:00
API : API ,
2014-05-16 06:29:42 +04:00
2014-07-21 21:50:04 +04:00
fork : fork ,
2014-07-24 13:46:05 +04:00
// Helpers to make it easier to write tests which are easy to read
2014-07-21 21:50:04 +04:00
context : {
2017-03-09 22:38:20 +03:00
internal : { context : { internal : true } } ,
external : { context : { external : true } } ,
owner : { context : { user : DataGenerator . Content . users [ 0 ] . id } } ,
admin : { context : { user : DataGenerator . Content . users [ 1 ] . id } } ,
editor : { context : { user : DataGenerator . Content . users [ 2 ] . id } } ,
author : { context : { user : DataGenerator . Content . users [ 3 ] . id } }
2014-07-24 13:46:05 +04:00
} ,
users : {
ids : {
✨ replace auto increment id's by object id (#7495)
* 🛠 bookshelf tarball, bson-objectid
* 🎨 schema changes
- change increment type to string
- add a default fallback for string length 191 (to avoid adding this logic to every single column which uses an ID)
- remove uuid, because ID now represents a global resource identifier
- keep uuid for post, because we are using this as preview id
- keep uuid for clients for now - we are using this param for Ghost-Auth
* ✨ base model: generate ObjectId on creating event
- each new resource get's a auto generate ObjectId
- this logic won't work for attached models, this commit comes later
* 🎨 centralised attach method
When attaching models there are two things important two know
1. To be able to attach an ObjectId, we need to register the `onCreating` event the fetched model!This is caused by the Bookshelf design in general. On this target model we are attaching the new model.
2. We need to manually fetch the target model, because Bookshelf has a weird behaviour (which is known as a bug, see see https://github.com/tgriesser/bookshelf/issues/629). The most important property when attaching a model is `parentFk`, which is the foreign key. This can be null when fetching the model with the option `withRelated`. To ensure quality and consistency, the custom attach wrapper always fetches the target model manual. By fetching the target model (again) is a little performance decrease, but it also has advantages: we can register the event, and directly unregister the event again. So very clean code.
Important: please only use the custom attach wrapper in the future.
* 🎨 token model had overriden the onCreating function because of the created_at field
- we need to ensure that the base onCreating hook get's triggered for ALL models
- if not, they don't get an ObjectId assigned
- in this case: be smart and check if the target model has a created_at field
* 🎨 we don't have a uuid field anymore, remove the usages
- no default uuid creation in models
- i am pretty sure we have some more definitions in our tests (for example in the export json files), but that is too much work to delete them all
* 🎨 do not parse ID to Number
- we had various occurances of parsing all ID's to numbers
- we don't need this behaviour anymore
- ID is string
- i will adapt the ID validation in the next commit
* 🎨 change ID regex for validation
- we only allow: ID as ObjectId, ID as 1 and ID as me
- we need to keep ID 1, because our whole software relies on ID 1 (permissions etc)
* 🎨 owner fixture
- roles: [4] does not work anymore
- 4 means -> static id 4
- this worked in an auto increment system (not even in a system with distributed writes)
- with ObjectId we generate each ID automatically (for static and dynamic resources)
- it is possible to define all id's for static resources still, but that means we need to know which ID is already used and for consistency we have to define ObjectId's for these static resources
- so no static id's anymore, except of: id 1 for owner and id 0 for external usage (because this is required from our permission system)
- NOTE: please read through the comment in the user model
* 🎨 tests: DataGenerator and test utils
First of all: we need to ensure using ObjectId's in the tests. When don't, we can't ensure that ObjectId's work properly.
This commit brings lot's of dynamic into all the static defined id's.
In one of the next commits, i will adapt all the tests.
* 🚨 remove counter in Notification API
- no need to add a counter
- we simply generate ObjectId's (they are auto incremental as well)
- our id validator does only allow ObjectId as id,1 and me
* 🎨 extend contextUser in Base Model
- remove isNumber check, because id's are no longer numbers, except of id 0/1
- use existing isExternalUser
- support id 0/1 as string or number
* ✨ Ghost Owner has id 1
- ensure we define this id in the fixtures.json
- doesn't matter if number or string
* 🎨 functional tests adaptions
- use dynamic id's
* 🎨 fix unit tests
* 🎨 integration tests adaptions
* 🎨 change importer utils
- all our export examples (test/fixtures/exports) contain id's as numbers
- fact: but we ignore them anyway when inserting into the database, see https://github.com/TryGhost/Ghost/blob/master/core/server/data/import/utils.js#L249
- in https://github.com/TryGhost/Ghost/pull/7495/commits/0e6ed957cd54dc02a25cf6fb1ab7d7e723295e2c#diff-70f514a06347c048648be464819503c4L67 i removed parsing id's to integers
- i realised that this ^ check just existed, because the userIdToMap was an object key and object keys are always strings!
- i think this logic is a little bit complicated, but i don't want to refactor this now
- this commit ensures when trying to find the user, the id comparison works again
- i've added more documentation to understand this logic ;)
- plus i renamed an attribute to improve readability
* 🎨 Data-Generator: add more defaults to createUser
- if i use the function DataGenerator.forKnex.createUser i would like to get a full set of defaults
* 🎨 test utils: change/extend function set for functional tests
- functional tests work a bit different
- they boot Ghost and seed the database
- some functional tests have mis-used the test setup
- the test setup needs two sections: integration/unit and functional tests
- any functional test is allowed to either add more data or change data in the existing Ghost db
- but what it should not do is: add test fixtures like roles or users from our DataGenerator and cross fingers it will work
- this commit adds a clean method for functional tests to add extra users
* 🎨 functional tests adaptions
- use last commit to insert users for functional tests clean
- tidy up usage of testUtils.setup or testUtils.doAuth
* 🐛 test utils: reset database before init
- ensure we don't have any left data from other tests in the database when starting ghost
* 🐛 fix test (unrelated to this PR)
- fixes a random failure
- return statement was missing
* 🎨 make changes for invites
2016-11-17 12:09:11 +03:00
owner : DataGenerator . Content . users [ 0 ] . id ,
admin : DataGenerator . Content . users [ 1 ] . id ,
editor : DataGenerator . Content . users [ 2 ] . id ,
2017-03-13 15:03:26 +03:00
author : DataGenerator . Content . users [ 3 ] . id ,
admin2 : DataGenerator . Content . users [ 6 ] . id ,
editor2 : DataGenerator . Content . users [ 4 ] . id ,
author2 : DataGenerator . Content . users [ 5 ] . id
2014-07-24 13:46:05 +04:00
}
} ,
roles : {
ids : {
✨ replace auto increment id's by object id (#7495)
* 🛠 bookshelf tarball, bson-objectid
* 🎨 schema changes
- change increment type to string
- add a default fallback for string length 191 (to avoid adding this logic to every single column which uses an ID)
- remove uuid, because ID now represents a global resource identifier
- keep uuid for post, because we are using this as preview id
- keep uuid for clients for now - we are using this param for Ghost-Auth
* ✨ base model: generate ObjectId on creating event
- each new resource get's a auto generate ObjectId
- this logic won't work for attached models, this commit comes later
* 🎨 centralised attach method
When attaching models there are two things important two know
1. To be able to attach an ObjectId, we need to register the `onCreating` event the fetched model!This is caused by the Bookshelf design in general. On this target model we are attaching the new model.
2. We need to manually fetch the target model, because Bookshelf has a weird behaviour (which is known as a bug, see see https://github.com/tgriesser/bookshelf/issues/629). The most important property when attaching a model is `parentFk`, which is the foreign key. This can be null when fetching the model with the option `withRelated`. To ensure quality and consistency, the custom attach wrapper always fetches the target model manual. By fetching the target model (again) is a little performance decrease, but it also has advantages: we can register the event, and directly unregister the event again. So very clean code.
Important: please only use the custom attach wrapper in the future.
* 🎨 token model had overriden the onCreating function because of the created_at field
- we need to ensure that the base onCreating hook get's triggered for ALL models
- if not, they don't get an ObjectId assigned
- in this case: be smart and check if the target model has a created_at field
* 🎨 we don't have a uuid field anymore, remove the usages
- no default uuid creation in models
- i am pretty sure we have some more definitions in our tests (for example in the export json files), but that is too much work to delete them all
* 🎨 do not parse ID to Number
- we had various occurances of parsing all ID's to numbers
- we don't need this behaviour anymore
- ID is string
- i will adapt the ID validation in the next commit
* 🎨 change ID regex for validation
- we only allow: ID as ObjectId, ID as 1 and ID as me
- we need to keep ID 1, because our whole software relies on ID 1 (permissions etc)
* 🎨 owner fixture
- roles: [4] does not work anymore
- 4 means -> static id 4
- this worked in an auto increment system (not even in a system with distributed writes)
- with ObjectId we generate each ID automatically (for static and dynamic resources)
- it is possible to define all id's for static resources still, but that means we need to know which ID is already used and for consistency we have to define ObjectId's for these static resources
- so no static id's anymore, except of: id 1 for owner and id 0 for external usage (because this is required from our permission system)
- NOTE: please read through the comment in the user model
* 🎨 tests: DataGenerator and test utils
First of all: we need to ensure using ObjectId's in the tests. When don't, we can't ensure that ObjectId's work properly.
This commit brings lot's of dynamic into all the static defined id's.
In one of the next commits, i will adapt all the tests.
* 🚨 remove counter in Notification API
- no need to add a counter
- we simply generate ObjectId's (they are auto incremental as well)
- our id validator does only allow ObjectId as id,1 and me
* 🎨 extend contextUser in Base Model
- remove isNumber check, because id's are no longer numbers, except of id 0/1
- use existing isExternalUser
- support id 0/1 as string or number
* ✨ Ghost Owner has id 1
- ensure we define this id in the fixtures.json
- doesn't matter if number or string
* 🎨 functional tests adaptions
- use dynamic id's
* 🎨 fix unit tests
* 🎨 integration tests adaptions
* 🎨 change importer utils
- all our export examples (test/fixtures/exports) contain id's as numbers
- fact: but we ignore them anyway when inserting into the database, see https://github.com/TryGhost/Ghost/blob/master/core/server/data/import/utils.js#L249
- in https://github.com/TryGhost/Ghost/pull/7495/commits/0e6ed957cd54dc02a25cf6fb1ab7d7e723295e2c#diff-70f514a06347c048648be464819503c4L67 i removed parsing id's to integers
- i realised that this ^ check just existed, because the userIdToMap was an object key and object keys are always strings!
- i think this logic is a little bit complicated, but i don't want to refactor this now
- this commit ensures when trying to find the user, the id comparison works again
- i've added more documentation to understand this logic ;)
- plus i renamed an attribute to improve readability
* 🎨 Data-Generator: add more defaults to createUser
- if i use the function DataGenerator.forKnex.createUser i would like to get a full set of defaults
* 🎨 test utils: change/extend function set for functional tests
- functional tests work a bit different
- they boot Ghost and seed the database
- some functional tests have mis-used the test setup
- the test setup needs two sections: integration/unit and functional tests
- any functional test is allowed to either add more data or change data in the existing Ghost db
- but what it should not do is: add test fixtures like roles or users from our DataGenerator and cross fingers it will work
- this commit adds a clean method for functional tests to add extra users
* 🎨 functional tests adaptions
- use last commit to insert users for functional tests clean
- tidy up usage of testUtils.setup or testUtils.doAuth
* 🐛 test utils: reset database before init
- ensure we don't have any left data from other tests in the database when starting ghost
* 🐛 fix test (unrelated to this PR)
- fixes a random failure
- return statement was missing
* 🎨 make changes for invites
2016-11-17 12:09:11 +03:00
owner : DataGenerator . Content . roles [ 3 ] . id ,
admin : DataGenerator . Content . roles [ 0 ] . id ,
editor : DataGenerator . Content . roles [ 1 ] . id ,
author : DataGenerator . Content . roles [ 2 ] . id
2014-07-24 13:46:05 +04:00
}
2014-07-28 17:19:49 +04:00
} ,
2014-09-25 13:35:28 +04:00
cacheRules : {
public : 'public, max-age=0' ,
2017-03-09 22:38:20 +03:00
hour : 'public, max-age=' + 3600 ,
2014-09-25 13:35:28 +04:00
day : 'public, max-age=' + 86400 ,
2017-03-09 22:38:20 +03:00
year : 'public, max-age=' + 31536000 ,
2014-09-25 13:35:28 +04:00
private : 'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0'
}
2013-10-08 05:39:33 +04:00
} ;