@@ -785,43 +785,77 @@ describe('angular', function() {
785785
786786
787787 describe ( 'csp' , function ( ) {
788+
789+ function mockCspElement ( cspAttrName , cspAttrValue ) {
790+ return spyOn ( document , 'querySelector' ) . andCallFake ( function ( selector ) {
791+ if ( selector == '[' + cspAttrName + ']' ) {
792+ var html = '<div ' + cspAttrName + ( cspAttrValue ? ( '="' + cspAttrValue + '" ' ) : '' ) + '></div>' ;
793+ return jqLite ( html ) [ 0 ] ;
794+ }
795+ } ) ;
796+
797+ }
798+
788799 var originalFunction ;
789800
790801 beforeEach ( function ( ) {
791- originalFunction = window . Function ;
802+ spyOn ( window , ' Function' ) ;
792803 } ) ;
793804
794805 afterEach ( function ( ) {
795- window . Function = originalFunction ;
796- delete csp . isActive_ ;
806+ delete csp . rules ;
797807 } ) ;
798808
799809
800- it ( 'should return the false when CSP is not enabled (the default)' , function ( ) {
801- expect ( csp ( ) ) . toBe ( false ) ;
810+ it ( 'should return the false for all rules when CSP is not enabled (the default)' , function ( ) {
811+ expect ( csp ( ) ) . toEqual ( { noUnsafeEval : false , noInlineStyle : false } ) ;
802812 } ) ;
803813
804814
805- it ( 'should return true if CSP is autodetected via CSP v1.1 securityPolicy.isActive property' , function ( ) {
806- window . Function = function ( ) { throw new Error ( 'CSP test' ) ; } ;
807- expect ( csp ( ) ) . toBe ( true ) ;
815+ it ( 'should return true for noUnsafeEval if eval causes a CSP security policy error' , function ( ) {
816+ window . Function . andCallFake ( function ( ) { throw new Error ( 'CSP test' ) ; } ) ;
817+ expect ( csp ( ) ) . toEqual ( { noUnsafeEval : true , noInlineStyle : false } ) ;
818+ expect ( window . Function ) . toHaveBeenCalledWith ( '' ) ;
808819 } ) ;
809820
810821
811- it ( 'should return the true when CSP is enabled manually via [ ng-csp] ' , function ( ) {
812- spyOn ( document , 'querySelector' ) . andCallFake ( function ( selector ) {
813- if ( selector == '[ng- csp]' ) return { } ;
814- } ) ;
815- expect ( csp ( ) ) . toBe ( true ) ;
822+ it ( 'should return true for all rules when CSP is enabled manually via empty ` ng-csp` attribute ' , function ( ) {
823+ var spy = mockCspElement ( 'ng-csp' ) ;
824+ expect ( csp ( ) ) . toEqual ( { noUnsafeEval : true , noInlineStyle : true } ) ;
825+ expect ( spy ) . toHaveBeenCalledWith ( '[ng-csp]' ) ;
826+ expect ( window . Function ) . not . toHaveBeenCalled ( ) ;
816827 } ) ;
817828
818829
819- it ( 'should return the true when CSP is enabled manually via [data-ng-csp]' , function ( ) {
820- spyOn ( document , 'querySelector' ) . andCallFake ( function ( selector ) {
821- if ( selector == '[data-ng-csp]' ) return { } ;
822- } ) ;
823- expect ( csp ( ) ) . toBe ( true ) ;
824- expect ( document . querySelector ) . toHaveBeenCalledWith ( '[data-ng-csp]' ) ;
830+ it ( 'should return true when CSP is enabled manually via [data-ng-csp]' , function ( ) {
831+ var spy = mockCspElement ( 'data-ng-csp' ) ;
832+ expect ( csp ( ) ) . toEqual ( { noUnsafeEval : true , noInlineStyle : true } ) ;
833+ expect ( spy ) . toHaveBeenCalledWith ( '[data-ng-csp]' ) ;
834+ expect ( window . Function ) . not . toHaveBeenCalled ( ) ;
835+ } ) ;
836+
837+
838+ it ( 'should return true for noUnsafeEval if it is specified in the `ng-csp` attribute value' , function ( ) {
839+ var spy = mockCspElement ( 'ng-csp' , 'no-unsafe-eval' ) ;
840+ expect ( csp ( ) ) . toEqual ( { noUnsafeEval : true , noInlineStyle : false } ) ;
841+ expect ( spy ) . toHaveBeenCalledWith ( '[ng-csp]' ) ;
842+ expect ( window . Function ) . not . toHaveBeenCalled ( ) ;
843+ } ) ;
844+
845+
846+ it ( 'should return true for noInlineStyle if it is specified in the `ng-csp` attribute value' , function ( ) {
847+ var spy = mockCspElement ( 'ng-csp' , 'no-inline-style' ) ;
848+ expect ( csp ( ) ) . toEqual ( { noUnsafeEval : false , noInlineStyle : true } ) ;
849+ expect ( spy ) . toHaveBeenCalledWith ( '[ng-csp]' ) ;
850+ expect ( window . Function ) . not . toHaveBeenCalled ( ) ;
851+ } ) ;
852+
853+
854+ it ( 'should return true for all styles if they are all specified in the `ng-csp` attribute value' , function ( ) {
855+ var spy = mockCspElement ( 'ng-csp' , 'no-inline-style;no-unsafe-eval' ) ;
856+ expect ( csp ( ) ) . toEqual ( { noUnsafeEval : true , noInlineStyle : true } ) ;
857+ expect ( spy ) . toHaveBeenCalledWith ( '[ng-csp]' ) ;
858+ expect ( window . Function ) . not . toHaveBeenCalled ( ) ;
825859 } ) ;
826860 } ) ;
827861
0 commit comments