@@ -2,66 +2,77 @@ var _ = require('lodash'),
2
2
url = require ( 'url' ) ,
3
3
moment = require ( 'moment' ) ,
4
4
config = require ( '../../server/config' ) ,
5
+ schema = require ( '../../server/data/schema' ) . tables ,
5
6
ApiRouteBase = '/ghost/api/v0.1/' ,
6
7
host = config . server . host ,
7
8
port = config . server . port ,
8
- schema = 'http://' ,
9
+ protocol = 'http://' ,
9
10
expectedProperties = {
11
+ // API top level
10
12
configuration : [ 'key' , 'value' , 'type' ] ,
11
- posts : [ 'posts' , 'meta' ] ,
12
- tags : [ 'tags' , 'meta' ] ,
13
- users : [ 'users' , 'meta' ] ,
14
- roles : [ 'roles '] ,
15
- pagination : [ 'page' , 'limit' , 'pages' , 'total' , 'next' , 'prev '] ,
16
- post : [ 'id' , 'uuid' , 'title ', 'slug ' , 'markdown ' , 'html ' , 'meta_title ' , 'meta_description' ,
17
- 'featured' , 'image' , 'status' , 'language' , 'created_at' , 'created_by' , 'updated_at' ,
18
- 'updated_by' , 'published_at' , 'published_by' , 'page' , 'author' , 'url'
19
- ] ,
20
- settings : [ 'settings' , 'meta' ] ,
21
- setting : [ 'id' , 'uuid' , 'key' , 'value' , 'type' , 'created_at' , 'created_by' , 'updated_at ', 'updated_by' ] ,
22
- tag : [ 'id' , 'uuid' , 'name' , 'slug' , 'description' , 'parent' , 'image' , 'hidden' ,
23
- 'meta_title' , 'meta_description' , 'created_at' , 'created_by' , 'updated_at' , 'updated_by'
24
- ] ,
25
- theme : [ 'uuid' , 'name' , 'version' , 'active' ] ,
26
- user : [ 'id' , 'uuid' , 'name' , 'slug' , 'email' , 'image' , 'cover' , 'bio' , 'website' ,
27
- 'location' , 'accessibility' , 'status' , 'language' , 'meta_title' , 'meta_description' , 'tour' , 'last_login' ,
28
- 'created_at' , 'created_by' , 'updated_at' , 'updated_by'
29
- ] ,
13
+ posts : [ 'posts' , 'meta' ] ,
14
+ tags : [ 'tags' , 'meta' ] ,
15
+ users : [ 'users' , 'meta' ] ,
16
+ settings : [ 'settings' , 'meta '] ,
17
+ roles : [ 'roles '] ,
18
+ pagination : [ 'page ', 'limit ' , 'pages ' , 'total ' , 'next ' , 'prev' ] ,
19
+ slugs : [ 'slugs' ] ,
20
+ slug : [ 'slug' ] ,
21
+ // object / model level
22
+ // Post API swaps author_id to author, and always returns a computed 'url' property
23
+ post : _ ( schema . posts ) . keys ( ) . without ( 'author_id' ) . concat ( 'author ', 'url' ) . value ( ) ,
24
+ // User API always removes the password field
25
+ user : _ ( schema . users ) . keys ( ) . without ( 'password' ) . value ( ) ,
26
+ // Tag API swaps parent_id to parent
27
+ tag : _ ( schema . tags ) . keys ( ) . without ( 'parent_id' ) . concat ( 'parent' ) . value ( ) ,
28
+ setting : _ . keys ( schema . settings ) ,
29
+ accesstoken : _ . keys ( schema . accesstokens ) ,
30
+ role : _ . keys ( schema . roles ) ,
31
+ permission : _ . keys ( schema . permissions ) ,
30
32
notification : [ 'type' , 'message' , 'status' , 'id' , 'dismissible' , 'location' ] ,
31
- slugs : [ 'slugs' ] ,
32
- slug : [ 'slug' ] ,
33
- accesstoken : [ 'access_token' , 'refresh_token' , 'expires_in' , 'token_type' ] ,
34
- role : [ 'id' , 'uuid' , 'name' , 'description' , 'created_at' , 'created_by' , 'updated_at' , 'updated_by' ] ,
35
- permission : [ 'id' , 'uuid' , 'name' , 'object_type' , 'action_type' , 'object_id' , 'created_at' , 'created_by' ,
36
- 'updated_at' , 'updated_by'
37
- ]
33
+ theme : [ 'uuid' , 'name' , 'version' , 'active' ]
38
34
} ;
39
35
40
36
function getApiQuery ( route ) {
41
37
return url . resolve ( ApiRouteBase , route ) ;
42
38
}
43
39
44
40
function getApiURL ( route ) {
45
- var baseURL = url . resolve ( schema + host + ':' + port , ApiRouteBase ) ;
41
+ var baseURL = url . resolve ( protocol + host + ':' + port , ApiRouteBase ) ;
46
42
return url . resolve ( baseURL , route ) ;
47
43
}
44
+
45
+ function getURL ( ) {
46
+ return protocol + host ;
47
+ }
48
+
48
49
function getSigninURL ( ) {
49
- return url . resolve ( schema + host + ':' + port , 'ghost/signin/' ) ;
50
+ return url . resolve ( protocol + host + ':' + port , 'ghost/signin/' ) ;
50
51
}
52
+
51
53
function getAdminURL ( ) {
52
- return url . resolve ( schema + host + ':' + port , 'ghost/' ) ;
54
+ return url . resolve ( protocol + host + ':' + port , 'ghost/' ) ;
55
+ }
56
+
57
+ function isISO8601 ( date ) {
58
+ return moment ( date ) . parsingFlags ( ) . iso ;
53
59
}
54
60
55
61
// make sure the API only returns expected properties only
56
- function checkResponseValue ( jsonResponse , properties ) {
57
- for ( var i = 0 ; i < properties . length ; i = i + 1 ) {
58
- // For some reason, settings response objects do not have the 'hasOwnProperty' method
59
- if ( Object . prototype . hasOwnProperty . call ( jsonResponse , properties [ i ] ) ) {
60
- continue ;
61
- }
62
- jsonResponse . should . have . property ( properties [ i ] ) ;
63
- }
64
- Object . keys ( jsonResponse ) . length . should . eql ( properties . length ) ;
62
+ function checkResponseValue ( jsonResponse , expectedProperties ) {
63
+ var providedProperties = _ . keys ( jsonResponse ) ,
64
+ missing = _ . difference ( expectedProperties , providedProperties ) ,
65
+ unexpected = _ . difference ( providedProperties , expectedProperties ) ;
66
+
67
+ _ . each ( missing , function ( prop ) {
68
+ jsonResponse . should . have . property ( prop ) ;
69
+ } ) ;
70
+
71
+ _ . each ( unexpected , function ( prop ) {
72
+ jsonResponse . should . not . have . property ( prop ) ;
73
+ } ) ;
74
+
75
+ providedProperties . length . should . eql ( expectedProperties . length ) ;
65
76
}
66
77
67
78
function checkResponse ( jsonResponse , objectType , additionalProperties , missingProperties ) {
@@ -72,14 +83,6 @@ function checkResponse(jsonResponse, objectType, additionalProperties, missingPr
72
83
checkResponseValue ( jsonResponse , checkProperties ) ;
73
84
}
74
85
75
- function isISO8601 ( date ) {
76
- return moment ( date ) . parsingFlags ( ) . iso ;
77
- }
78
-
79
- function getURL ( ) {
80
- return schema + host ;
81
- }
82
-
83
86
module . exports = {
84
87
getApiURL : getApiURL ,
85
88
getApiQuery : getApiQuery ,
0 commit comments