2020-08-07 16:12:34 +02:00
package com . sap . piper
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.ExpectedException
import org.junit.rules.RuleChain
import util.BasePiperTest
import util.JenkinsCredentialsRule
import util.JenkinsFileExistsRule
import util.JenkinsLoggingRule
import util.JenkinsReadFileRule
import util.JenkinsReadYamlRule
import util.JenkinsShellCallRule
import util.JenkinsWriteFileRule
import util.Rules
import static org . junit . Assert . assertThat
import static org . hamcrest . Matchers . *
import static org . junit . Assert . assertTrue
class TemporaryCredentialsUtilsTest extends BasePiperTest {
private ExpectedException thrown = ExpectedException . none ( )
private JenkinsCredentialsRule credentialsRule = new JenkinsCredentialsRule ( this )
private JenkinsWriteFileRule writeFileRule = new JenkinsWriteFileRule ( this )
private JenkinsFileExistsRule fileExistsRule = new JenkinsFileExistsRule ( this , [ ] )
private JenkinsReadFileRule readFileRule = new JenkinsReadFileRule ( this , null )
private JenkinsReadYamlRule readYamlRule = new JenkinsReadYamlRule ( this )
private JenkinsShellCallRule shellRule = new JenkinsShellCallRule ( this )
private JenkinsLoggingRule loggingRule = new JenkinsLoggingRule ( this )
def bodyExecuted
TemporaryCredentialsUtils credUtils
@Rule
public RuleChain ruleChain = Rules
. getCommonRules ( this )
. around ( thrown )
. around ( readYamlRule )
. around ( credentialsRule )
. around ( loggingRule )
. around ( writeFileRule )
. around ( fileExistsRule )
. around ( readFileRule )
. around ( shellRule )
@Before
void init ( ) {
bodyExecuted = false
credentialsRule . reset ( )
. withCredentials ( 'erp-credentials' , 'test_user' , '********' )
. withCredentials ( 'testCred2' , 'test_other' , '**' )
credUtils = new TemporaryCredentialsUtils ( nullScript )
}
@Test
2020-08-14 15:22:05 +02:00
void singleCredentialsFileWritten ( ) {
2020-08-07 16:12:34 +02:00
def credential = [ alias: 'ERP' , credentialId: 'erp-credentials' ]
2020-08-14 15:22:05 +02:00
def directories = [ './' , 'integration-tests/src/test/resources' ]
2020-08-07 16:12:34 +02:00
def filename = 'credentials.json'
2020-08-14 15:22:05 +02:00
fileExistsRule . registerExistingFile ( './systems.yml' )
2020-08-07 16:12:34 +02:00
2020-08-14 15:22:05 +02:00
credUtils . writeCredentials ( [ credential ] , directories , filename )
2020-08-07 16:12:34 +02:00
2020-08-14 15:22:05 +02:00
assertThat ( writeFileRule . files [ './credentials.json' ] , containsString ( '"alias":"ERP","username":"test_user","password":"********"' ) )
2020-08-07 16:12:34 +02:00
}
@Test
2020-08-14 15:22:05 +02:00
void twoCredentialsFilesWritten ( ) {
def credential = [ alias: 'ERP' , credentialId: 'erp-credentials' ]
def directories = [ './' , 'integration-tests/src/test/resources' ]
2020-08-07 16:12:34 +02:00
def filename = 'credentials.json'
2020-08-14 15:22:05 +02:00
fileExistsRule . registerExistingFile ( './systems.yml' )
fileExistsRule . registerExistingFile ( 'integration-tests/src/test/resources/systems.yml' )
2020-08-07 16:12:34 +02:00
2020-08-14 15:22:05 +02:00
credUtils . writeCredentials ( [ credential ] , directories , filename )
2020-08-07 16:12:34 +02:00
2020-08-14 15:22:05 +02:00
assertThat ( writeFileRule . files [ "./credentials.json" ] , containsString ( '"alias":"ERP","username":"test_user","password":"********"' ) )
assertThat ( writeFileRule . files [ "integration-tests/src/test/resources/credentials.json" ] , containsString ( '"alias":"ERP","username":"test_user","password":"********"' ) )
2020-08-07 16:12:34 +02:00
}
@Test
2020-08-14 15:22:05 +02:00
void credentialsFileNotWrittenWithEmptyList ( ) {
def directories = [ './' , 'integration-tests/src/test/resources' ]
2020-08-07 16:12:34 +02:00
def filename = 'credentials.json'
fileExistsRule . registerExistingFile ( 'systems.yml' )
2020-08-14 15:22:05 +02:00
credUtils . writeCredentials ( [ ] , directories , filename )
2020-08-07 16:12:34 +02:00
2020-08-14 15:22:05 +02:00
loggingRule . expect ( 'Not writing any credentials.' )
2020-08-07 16:12:34 +02:00
}
@Test
void systemsFileNotExists ( ) {
2020-08-14 15:22:05 +02:00
def credential = [ alias: 'ERP' , credentialId: 'erp-credentials' ]
def directories = [ './' , 'integration-tests/src/test/resources' ]
def filename = 'credentials.json'
2020-08-07 16:12:34 +02:00
thrown . expect ( hudson . AbortException )
2020-08-14 15:22:05 +02:00
thrown . expectMessage ( "None of the directories [./, integration-tests/src/test/resources/] contains any of the files systems.yml, systems.yaml or systems.json. " +
2020-08-07 16:12:34 +02:00
"One of those files is required in order to activate the integration test credentials configured in the pipeline configuration file of this project. " +
2020-11-23 18:48:53 +02:00
"Please add the file as explained in project 'Piper' documentation." )
2020-08-07 16:12:34 +02:00
2020-08-14 15:22:05 +02:00
credUtils . writeCredentials ( [ credential ] , directories , filename )
}
@Test
void credentialsFileDeleted ( ) {
def directories = [ './' , 'integration-tests/src/test/resources' ]
def filename = 'credentials.json'
fileExistsRule . registerExistingFile ( 'systems.yml' )
fileExistsRule . registerExistingFile ( './credentials.json' )
credUtils . deleteCredentials ( directories , filename )
assertThat ( shellRule . shell , hasItem ( 'rm -f ./credentials.json' ) )
2020-08-07 16:12:34 +02:00
}
@Test
void handleTemporaryCredentials ( ) {
def credential = [ alias: 'ERP' , credentialId: 'erp-credentials' ]
2020-08-14 15:22:05 +02:00
def directories = [ './' , 'integration-tests/src/test/resources' ]
fileExistsRule . registerExistingFile ( './systems.yml' )
fileExistsRule . registerExistingFile ( './credentials.json' )
2020-08-07 16:12:34 +02:00
2020-08-14 15:22:05 +02:00
credUtils . handleTemporaryCredentials ( [ credential ] , directories ) {
2020-08-07 16:12:34 +02:00
bodyExecuted = true
}
assertTrue ( bodyExecuted )
2020-08-14 15:22:05 +02:00
assertThat ( writeFileRule . files [ './credentials.json' ] , containsString ( '"alias":"ERP","username":"test_user","password":"********"' ) )
assertThat ( shellRule . shell , hasItem ( 'rm -f ./credentials.json' ) )
2020-08-07 16:12:34 +02:00
}
@Test
2020-08-14 15:22:05 +02:00
void handleTemporaryCredentialsNoDirectories ( ) {
2020-08-07 16:12:34 +02:00
thrown . expect ( hudson . AbortException )
2020-08-14 15:22:05 +02:00
thrown . expectMessage ( "This should not happen: Directories for credentials files not specified." )
2020-08-07 16:12:34 +02:00
2020-08-14 15:22:05 +02:00
credUtils . handleTemporaryCredentials ( [ ] , [ ] ) {
2020-08-07 16:12:34 +02:00
bodyExecuted = true
}
}
@Test
void handleTemporaryCredentialsNoCredentials ( ) {
2020-08-14 15:22:05 +02:00
def directories = [ './' , 'integration-tests/src/test/resources' ]
credUtils . handleTemporaryCredentials ( [ ] , directories ) {
2020-08-07 16:12:34 +02:00
bodyExecuted = true
}
assertTrue ( bodyExecuted )
assertThat ( writeFileRule . files . keySet ( ) , hasSize ( 0 ) )
assertThat ( shellRule . shell , hasSize ( 0 ) )
}
}