Blame view

keycloak_test.groovy 2.14 KB
866567c3c   George Vlahavas   Add keycloak fail...
1
  /**
49c2f0537   George Vlahavas   Fix comments
2
3
4
5
6
  * Jenkinsfile to run keycloak tests on a keycloak enabled app.
  * An instance of the application should already be deployed. Two tests
  * are included, one that tries to access an API endpoint without any
  * authentication and should fail and a second one that uses
  * authentication and should succeed.
866567c3c   George Vlahavas   Add keycloak fail...
7
8
9
10
11
12
13
14
  **/
  
  
  pipeline {
  
    agent any
  
      environment {
866567c3c   George Vlahavas   Add keycloak fail...
15
        
866567c3c   George Vlahavas   Add keycloak fail...
16
        APP_URL = "https://kea.curex-project.eu"
972595bcc   George Vlahavas   Change Master to ...
17
        KEYCLOAK_URL = "https://keycloak.curex-project.eu/auth/realms/master/protocol/openid-connect/token"
3d2811c1f   George Vlahavas   Remove deployment
18
19
        USER = credentials('keycloak-test-user')
        PASSWORD = credentials('keycloak-test-password')
7695d6fdb   George Vlahavas   Use a variable fo...
20
        CLIENT_ID = "KEA"
866567c3c   George Vlahavas   Add keycloak fail...
21

866567c3c   George Vlahavas   Add keycloak fail...
22
23
24
25
      }
  
    stages {
             
866567c3c   George Vlahavas   Add keycloak fail...
26
27
28
29
30
31
32
      stage('Run tests') {
        steps {
          script {
            echo '*************'
            echo '*** TESTS ***'
            echo '*************'
            /* Here do your tests */
866567c3c   George Vlahavas   Add keycloak fail...
33
34
35
36
37
38
39
            try {
              String testName = "KEA_keycloak_fail"
              String url = "$APP_URL/api/v1/od/status"
              String responseCode = sh(label: testName, script: "curl -m 10 -sLI -w '%{http_code}' -H 'Accept: application/json, text/plain, */*' $url -o /dev/null", returnStdout: true)
              if ( responseCode != '401' ) {
                error("$testName: Returned status code = $responseCode when calling $url")
              }
9dd323112   George Vlahavas   Add succesfull ke...
40
41
  
              testName = "KEA_keycloak_success"
7695d6fdb   George Vlahavas   Use a variable fo...
42
              String token = sh(label: "get_keycloak_token", script: "curl -s -X POST $KEYCLOAK_URL -H 'Content-Type: application/x-www-form-urlencoded' -d 'username=$USER' -d 'password=$PASSWORD' -d 'grant_type=password'  -d 'client_id=$CLIENT_ID' | sed 's/.*access_token\":\"//g' | sed 's/\".*//g'", returnStdout: true)
9dd323112   George Vlahavas   Add succesfull ke...
43
              responseCode = sh(label: testName, script: "curl -m 10 -sLI -w '%{http_code}' -H 'Authorization: Bearer $token' -H 'Accept: application/json, text/plain, */*' $url -o /dev/null", returnStdout: true)
f0b7c788b   George Vlahavas   Response code sho...
44
              if ( responseCode != '200' ) {
9dd323112   George Vlahavas   Add succesfull ke...
45
46
                error("$testName: Returned status code = $responseCode when calling $url")
              }
971ec8495   George Vlahavas   Add catch statement
47
48
49
            } catch (ignored) {
              currentBuild.result = 'FAILURE'
              echo "KEA Keycloak Tests failed"
866567c3c   George Vlahavas   Add keycloak fail...
50
51
52
53
            }
          }
        }
      }
866567c3c   George Vlahavas   Add keycloak fail...
54
55
    }
  }