2018-05-11 08:12:15 +03:00
const should = require ( 'should' ) ;
const sinon = require ( 'sinon' ) ;
const _ = require ( 'lodash' ) ;
2020-03-30 18:26:47 +03:00
const validate = require ( '../../../../core/frontend/services/themes/validate' ) ;
2018-05-11 08:12:15 +03:00
const gscan = require ( 'gscan' ) ;
describe ( 'Themes' , function ( ) {
let checkZipStub ;
let checkStub ;
let formatStub ;
beforeEach ( function ( ) {
2019-01-21 19:53:44 +03:00
checkZipStub = sinon . stub ( gscan , 'checkZip' ) ;
checkStub = sinon . stub ( gscan , 'check' ) ;
formatStub = sinon . stub ( gscan , 'format' ) ;
2018-05-11 08:12:15 +03:00
} ) ;
afterEach ( function ( ) {
2019-01-21 19:53:44 +03:00
sinon . restore ( ) ;
2018-05-11 08:12:15 +03:00
} ) ;
describe ( 'Validate' , function ( ) {
const testTheme = {
name : 'supertheme' ,
version : '1.0.0' ,
path : '/path/to/theme'
} ;
it ( '[success] validates a valid zipped theme' , function ( ) {
checkZipStub . resolves ( { } ) ;
formatStub . returns ( { results : { error : [ ] } } ) ;
return validate . check ( testTheme , true )
. then ( ( checkedTheme ) => {
checkZipStub . calledOnce . should . be . true ( ) ;
checkZipStub . calledWith ( testTheme ) . should . be . true ( ) ;
checkStub . callCount . should . be . equal ( 0 ) ;
formatStub . calledOnce . should . be . true ( ) ;
checkedTheme . should . be . an . Object ( ) ;
2019-04-22 18:31:56 +03:00
should . equal ( validate . canActivate ( checkedTheme ) , true ) ;
2018-05-11 08:12:15 +03:00
} ) ;
} ) ;
it ( '[success] validates a valid unzipped theme' , function ( ) {
checkStub . resolves ( { } ) ;
formatStub . returns ( { results : { error : [ ] } } ) ;
return validate . check ( testTheme , false )
. then ( ( checkedTheme ) => {
checkZipStub . callCount . should . be . equal ( 0 ) ;
checkStub . calledOnce . should . be . true ( ) ;
checkStub . calledWith ( testTheme . path ) . should . be . true ( ) ;
formatStub . calledOnce . should . be . true ( ) ;
checkedTheme . should . be . an . Object ( ) ;
2019-04-22 18:31:56 +03:00
should . equal ( validate . canActivate ( checkedTheme ) , true ) ;
2018-05-11 08:12:15 +03:00
} ) ;
} ) ;
it ( '[failure] validates an invalid zipped theme' , function ( ) {
checkZipStub . resolves ( { } ) ;
formatStub . returns ( {
results : {
error : [
{
fatal : true ,
level : 'error' ,
rule : 'Replace the <code>{{#if author.cover}}</code> helper with <code>{{#if author.cover_image}}</code>' ,
2019-07-25 10:17:23 +03:00
details : 'The <code>cover</code> attribute was replaced with <code>cover_image</code>.<br>Instead of <code>{{#if author.cover}}</code> you need to use <code>{{#if author.cover_image}}</code>.<br>See the object attributes of <code>author</code> <a href="https://ghost.org/docs/api/handlebars-themes/context/author/#author-object-attributes" target=_blank>here</a>.' ,
2019-07-05 14:40:43 +03:00
failures : [ { } ] ,
2018-05-11 08:12:15 +03:00
code : 'GS001-DEPR-CON-AC'
}
]
}
} ) ;
return validate . check ( testTheme , true )
. then ( ( checkedTheme ) => {
checkZipStub . calledOnce . should . be . true ( ) ;
checkZipStub . calledWith ( testTheme ) . should . be . true ( ) ;
checkStub . callCount . should . be . equal ( 0 ) ;
formatStub . calledOnce . should . be . true ( ) ;
2019-04-22 18:31:56 +03:00
should . equal ( validate . canActivate ( checkedTheme ) , false ) ;
2018-05-11 08:12:15 +03:00
} ) ;
} ) ;
it ( '[failure] validates an invalid unzipped theme' , function ( ) {
checkStub . resolves ( { } ) ;
formatStub . returns ( {
results : {
error : [
{
fatal : true ,
level : 'error' ,
rule : 'Replace the <code>{{#if author.cover}}</code> helper with <code>{{#if author.cover_image}}</code>' ,
2019-07-25 10:17:23 +03:00
details : 'The <code>cover</code> attribute was replaced with <code>cover_image</code>.<br>Instead of <code>{{#if author.cover}}</code> you need to use <code>{{#if author.cover_image}}</code>.<br>See the object attributes of <code>author</code> <a href="https://ghost.org/docs/api/handlebars-themes/context/author/#author-object-attributes" target=_blank>here</a>.' ,
2019-07-05 14:40:43 +03:00
failures : [ { } ] ,
2018-05-11 08:12:15 +03:00
code : 'GS001-DEPR-CON-AC'
}
]
}
} ) ;
return validate . check ( testTheme , false )
. then ( ( checkedTheme ) => {
checkStub . calledOnce . should . be . true ( ) ;
checkStub . calledWith ( testTheme . path ) . should . be . true ( ) ;
checkZipStub . callCount . should . be . equal ( 0 ) ;
formatStub . calledOnce . should . be . true ( ) ;
2019-04-22 18:31:56 +03:00
should . equal ( validate . canActivate ( checkedTheme ) , false ) ;
2018-05-11 08:12:15 +03:00
} ) ;
} ) ;
it ( '[failure] can handle a corrupt zip file' , function ( ) {
checkZipStub . rejects ( new Error ( 'invalid zip file' ) ) ;
formatStub . returns ( { results : { error : [ ] } } ) ;
return validate . check ( testTheme , true )
. then ( ( checkedTheme ) => {
checkedTheme . should . not . exist ( ) ;
} ) . catch ( ( error ) => {
error . should . be . an . Object ( ) ;
error . message . should . be . equal ( 'invalid zip file' ) ;
checkZipStub . calledOnce . should . be . true ( ) ;
checkZipStub . calledWith ( testTheme ) . should . be . true ( ) ;
checkStub . callCount . should . be . equal ( 0 ) ;
formatStub . calledOnce . should . be . false ( ) ;
} ) ;
} ) ;
} ) ;
} ) ;