2020-03-02 20:52:38 +08:00
import {
isNullOrUndefined ,
generateTokenType ,
generateRepositoryPath ,
suppressSensitiveInformation
2020-03-05 21:19:45 +08:00
} from '../src/util'
2019-11-19 23:06:27 +08:00
2020-03-05 21:19:45 +08:00
describe ( 'util' , ( ) = > {
describe ( 'isNullOrUndefined' , ( ) = > {
it ( 'should return true if the value is null' , async ( ) = > {
const value = null
expect ( isNullOrUndefined ( value ) ) . toBeTruthy ( )
} )
2020-01-12 09:26:08 +08:00
2020-03-05 21:19:45 +08:00
it ( 'should return true if the value is undefined' , async ( ) = > {
const value = undefined
expect ( isNullOrUndefined ( value ) ) . toBeTruthy ( )
} )
2020-01-12 09:26:08 +08:00
2020-03-05 21:19:45 +08:00
it ( 'should return false if the value is defined' , async ( ) = > {
const value = 'montezuma'
expect ( isNullOrUndefined ( value ) ) . toBeFalsy ( )
} )
} )
2020-03-02 20:52:38 +08:00
2020-03-05 21:19:45 +08:00
describe ( 'generateTokenType' , ( ) = > {
it ( 'should return ssh if ssh is provided' , async ( ) = > {
2020-03-02 20:52:38 +08:00
const action = {
2020-03-05 21:19:45 +08:00
branch : '123' ,
root : '.' ,
workspace : 'src/' ,
folder : 'build' ,
2020-03-02 20:52:38 +08:00
gitHubToken : null ,
accessToken : null ,
ssh : true
2020-03-05 21:19:45 +08:00
}
expect ( generateTokenType ( action ) ) . toEqual ( 'SSH Deploy Key' )
} )
2020-03-02 20:52:38 +08:00
2020-03-05 21:19:45 +08:00
it ( 'should return access token if access token is provided' , async ( ) = > {
2020-03-02 20:52:38 +08:00
const action = {
2020-03-05 21:19:45 +08:00
branch : '123' ,
root : '.' ,
workspace : 'src/' ,
folder : 'build' ,
2020-03-02 20:52:38 +08:00
gitHubToken : null ,
2020-03-05 21:19:45 +08:00
accessToken : '123' ,
2020-03-29 01:22:39 +08:00
ssh : false
2020-03-05 21:19:45 +08:00
}
expect ( generateTokenType ( action ) ) . toEqual ( 'Access Token' )
} )
2020-03-02 20:52:38 +08:00
2020-03-05 21:19:45 +08:00
it ( 'should return github token if github token is provided' , async ( ) = > {
2020-03-02 20:52:38 +08:00
const action = {
2020-03-05 21:19:45 +08:00
branch : '123' ,
root : '.' ,
workspace : 'src/' ,
folder : 'build' ,
gitHubToken : '123' ,
2020-03-02 20:52:38 +08:00
accessToken : null ,
2020-03-29 01:22:39 +08:00
ssh : false
2020-03-05 21:19:45 +08:00
}
expect ( generateTokenType ( action ) ) . toEqual ( 'GitHub Token' )
} )
2020-03-02 20:52:38 +08:00
2020-03-05 21:19:45 +08:00
it ( 'should return ... if no token is provided' , async ( ) = > {
2020-03-02 20:52:38 +08:00
const action = {
2020-03-05 21:19:45 +08:00
branch : '123' ,
root : '.' ,
workspace : 'src/' ,
folder : 'build' ,
2020-03-02 20:52:38 +08:00
gitHubToken : null ,
accessToken : null ,
2020-03-29 01:22:39 +08:00
ssh : false
2020-03-05 21:19:45 +08:00
}
expect ( generateTokenType ( action ) ) . toEqual ( '...' )
} )
} )
2020-03-02 20:52:38 +08:00
2020-03-05 21:19:45 +08:00
describe ( 'generateRepositoryPath' , ( ) = > {
it ( 'should return ssh if ssh is provided' , async ( ) = > {
2020-03-02 20:52:38 +08:00
const action = {
2020-03-05 21:19:45 +08:00
repositoryName : 'JamesIves/github-pages-deploy-action' ,
branch : '123' ,
root : '.' ,
workspace : 'src/' ,
folder : 'build' ,
2020-03-02 20:52:38 +08:00
gitHubToken : null ,
accessToken : null ,
ssh : true
2020-03-05 21:19:45 +08:00
}
2020-03-02 20:52:38 +08:00
expect ( generateRepositoryPath ( action ) ) . toEqual (
2020-03-05 21:19:45 +08:00
'git@github.com:JamesIves/github-pages-deploy-action'
)
} )
2020-03-02 20:52:38 +08:00
2020-03-05 21:19:45 +08:00
it ( 'should return https if access token is provided' , async ( ) = > {
2020-03-02 20:52:38 +08:00
const action = {
2020-03-05 21:19:45 +08:00
repositoryName : 'JamesIves/github-pages-deploy-action' ,
branch : '123' ,
root : '.' ,
workspace : 'src/' ,
folder : 'build' ,
2020-03-02 20:52:38 +08:00
gitHubToken : null ,
2020-03-05 21:19:45 +08:00
accessToken : '123' ,
2020-03-29 01:22:39 +08:00
ssh : false
2020-03-05 21:19:45 +08:00
}
2020-03-02 20:52:38 +08:00
expect ( generateRepositoryPath ( action ) ) . toEqual (
2020-03-05 21:19:45 +08:00
'https://123@github.com/JamesIves/github-pages-deploy-action.git'
)
} )
2020-03-02 20:52:38 +08:00
2020-03-05 21:19:45 +08:00
it ( 'should return https with x-access-token if github token is provided' , async ( ) = > {
2020-03-02 20:52:38 +08:00
const action = {
2020-03-05 21:19:45 +08:00
repositoryName : 'JamesIves/github-pages-deploy-action' ,
branch : '123' ,
root : '.' ,
workspace : 'src/' ,
folder : 'build' ,
gitHubToken : '123' ,
2020-03-02 20:52:38 +08:00
accessToken : null ,
2020-03-29 01:22:39 +08:00
ssh : false
2020-03-05 21:19:45 +08:00
}
2020-03-02 20:52:38 +08:00
expect ( generateRepositoryPath ( action ) ) . toEqual (
2020-03-05 21:19:45 +08:00
'https://x-access-token:123@github.com/JamesIves/github-pages-deploy-action.git'
)
} )
2020-03-02 20:52:38 +08:00
2020-03-05 21:19:45 +08:00
describe ( 'suppressSensitiveInformation' , ( ) = > {
it ( 'should replace any sensitive information with ***' , async ( ) = > {
2020-03-02 20:52:38 +08:00
const action = {
2020-03-05 21:19:45 +08:00
repositoryName : 'JamesIves/github-pages-deploy-action' ,
2020-03-02 20:52:38 +08:00
repositoryPath :
2020-03-05 21:19:45 +08:00
'https://x-access-token:supersecret999%%%@github.com/anothersecret123333' ,
branch : '123' ,
root : '.' ,
workspace : 'src/' ,
folder : 'build' ,
accessToken : 'supersecret999%%%' ,
gitHubToken : 'anothersecret123333'
}
2020-03-02 20:52:38 +08:00
2020-03-05 21:19:45 +08:00
const string = ` This is an error message! It contains ${ action . accessToken } and ${ action . gitHubToken } and ${ action . repositoryPath } `
2020-03-02 20:52:38 +08:00
expect ( suppressSensitiveInformation ( string , action ) ) . toBe (
2020-03-05 21:19:45 +08:00
'This is an error message! It contains *** and *** and ***'
)
} )
2020-03-02 20:52:38 +08:00
2020-03-05 21:19:45 +08:00
it ( 'should not suppress information when in debug mode' , async ( ) = > {
2020-03-02 20:52:38 +08:00
const action = {
2020-03-05 21:19:45 +08:00
repositoryName : 'JamesIves/github-pages-deploy-action' ,
2020-03-02 20:52:38 +08:00
repositoryPath :
2020-03-05 21:19:45 +08:00
'https://x-access-token:supersecret999%%%@github.com/anothersecret123333' ,
branch : '123' ,
root : '.' ,
workspace : 'src/' ,
folder : 'build' ,
accessToken : 'supersecret999%%%' ,
gitHubToken : 'anothersecret123333'
}
2020-03-02 20:52:38 +08:00
2020-03-28 22:35:26 +08:00
process . env [ 'RUNNER_DEBUG' ] = '1'
2020-03-02 20:52:38 +08:00
2020-03-05 21:19:45 +08:00
const string = ` This is an error message! It contains ${ action . accessToken } and ${ action . gitHubToken } and ${ action . repositoryPath } `
2020-03-02 20:52:38 +08:00
expect ( suppressSensitiveInformation ( string , action ) ) . toBe (
2020-03-05 21:19:45 +08:00
'This is an error message! It contains supersecret999%%% and anothersecret123333 and https://x-access-token:supersecret999%%%@github.com/anothersecret123333'
)
} )
} )
} )
} )