Skip to content
This repository was archived by the owner on Jul 17, 2018. It is now read-only.

Commit 09d43b1

Browse files
author
Darren Jones
authored
Merge pull request #143 from dazjones/refactored-tests
Refactored tests
2 parents 537ccc2 + 8965a7c commit 09d43b1

File tree

8 files changed

+260
-163
lines changed

8 files changed

+260
-163
lines changed

ci/tests/jenkinstest/auth_test.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
"os"
7+
"strings"
8+
)
9+
10+
const LOGIN_URL string = "/login"
11+
12+
var loginScreenBody string
13+
var validLoginAttemptBody string
14+
var invalidLoginAttemptBody string
15+
16+
func thereIsAJenkinsInstall() error {
17+
// TODO - Write a sensible test
18+
return nil
19+
}
20+
21+
func iAccessTheLoginScreen() error {
22+
resp, err := http.Get(getUrl(LOGIN_URL))
23+
if err != nil {
24+
return err
25+
}
26+
27+
loginScreenBody, _ = getBodyString(resp)
28+
return nil
29+
}
30+
31+
func jenkinsShouldBeUnlocked() error {
32+
if strings.Contains(loginScreenBody, "Unlock Jenkins") {
33+
return fmt.Errorf("expected %s not to contain 'Unlock Jenkins'", loginScreenBody)
34+
}
35+
return nil
36+
}
37+
38+
func iLoginUsingValidCredentials() error {
39+
validLoginAttemptBody, _ = loginToJenkins(os.Getenv("JENKINS_PASSWORD"))
40+
return nil
41+
}
42+
43+
func iLoginUsingInvalidCredentials() error {
44+
invalidLoginAttemptBody, _ = loginToJenkins("garbagepasswordthatwontwork123")
45+
return nil
46+
}
47+
48+
func loginToJenkins(password string) (string, error) {
49+
if password == "" {
50+
return "", fmt.Errorf("%s", "Empty password")
51+
}
52+
53+
body, err := jenkinsLogin("administrator", password)
54+
55+
if err != nil {
56+
return "ERROR", err
57+
}
58+
59+
return body, nil
60+
}
61+
62+
func iAmLoggedIn() error {
63+
if !strings.Contains(string(validLoginAttemptBody), `<a href="/logout"><b>log out</b></a>`) {
64+
return fmt.Errorf("expected %s to contain '/logout' link", validLoginAttemptBody)
65+
}
66+
67+
return nil
68+
}
69+
70+
func iAmNotLoggedIn() error {
71+
if strings.Contains(string(invalidLoginAttemptBody), `<a href="/logout"><b>log out</b></a>`) {
72+
return fmt.Errorf("expected %s to not contain '/logout' link", invalidLoginAttemptBody)
73+
}
74+
75+
return nil
76+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Feature: Jenkins authentication
2+
In order to use Jenkins
3+
As a user
4+
I need to be able to login
5+
6+
Scenario: Can access login screen
7+
Given there is a Jenkins install
8+
When I access the login screen
9+
Then Jenkins should be unlocked
10+
11+
Scenario: Invalid user can't log in
12+
Given I access the login screen
13+
When I login using invalid credentials
14+
Then I am not logged in
15+
16+
Scenario: Can log in
17+
Given I access the login screen
18+
When I login using valid credentials
19+
Then I am logged in

ci/tests/jenkinstest/features/jenkins.feature

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Feature: Jenkins plugin installation and configuration
2+
In order to use Jenkins plugins
3+
As an administrator
4+
I need to be able to install and configure plugins
5+
6+
Scenario: Plugins are installed
7+
Given I login using valid credentials
8+
When I access plugin management
9+
Then all the plugins are installed

ci/tests/jenkinstest/godogs.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,25 @@
1-
/* file: $GOPATH/src/godogs/godogs.go */
21
package main
32

4-
// Godogs available to eat
5-
var Godogs int
3+
import (
4+
"net/http"
5+
"os"
6+
)
67

7-
func main() { /* usual main func */ }
8+
const (
9+
JENKINS_HOST string = "JENKINS_HOST"
10+
)
11+
12+
var jenkinsHostUrl string
13+
var httpClient *http.Client
14+
15+
func init() {
16+
jenkinsHostUrl = os.Getenv(JENKINS_HOST)
17+
18+
if jenkinsHostUrl == "" {
19+
panic("JENKINS_HOST is empty")
20+
}
21+
}
22+
23+
func main() {
24+
25+
}
Lines changed: 11 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -1,155 +1,21 @@
11
package main
22

33
import (
4-
"encoding/json"
5-
"fmt"
64
"github.com/DATA-DOG/godog"
7-
"io/ioutil"
8-
"net/http"
9-
"net/http/cookiejar"
10-
"net/url"
11-
"os"
12-
"strings"
135
)
146

15-
const (
16-
JENKINS_HOST string = "JENKINS_HOST"
17-
)
18-
19-
var jenkinsHostUrl string = os.Getenv(JENKINS_HOST)
20-
var jenkinsLogin string = jenkinsHostUrl + "/login"
21-
var requestUrl string
22-
var body string
23-
var pluginsResp string
24-
var cookieJar *cookiejar.Jar
25-
var crumb JenkinsCrumb
26-
var httpClient *http.Client
27-
28-
type JenkinsCrumb struct {
29-
Crumb string `json:"crumb"`
30-
CrumbRequestField string `json: "crumbRequestField"`
31-
}
32-
33-
func init() {
34-
createNewHttpClient()
35-
}
36-
37-
func createNewCookieJar() {
38-
cookieJar, _ = cookiejar.New(&cookiejar.Options{})
39-
}
40-
41-
func createNewHttpClient() {
42-
createNewCookieJar()
43-
44-
httpClient = &http.Client{
45-
Jar: cookieJar,
46-
}
47-
}
48-
49-
func thereIsAJenkinsInstall() error {
50-
requestUrl = jenkinsHostUrl + "/login"
51-
return nil
52-
}
53-
54-
func getBodyString(resp *http.Response)(string, error) {
55-
defer resp.Body.Close()
56-
body_bytes, err := ioutil.ReadAll(resp.Body)
57-
58-
if err != nil {
59-
return "", fmt.Errorf("%s", err)
60-
}
61-
62-
return string(body_bytes), nil
63-
}
64-
65-
func iAccessTheLoginScreen() error {
66-
resp, err := http.Get(requestUrl)
67-
if err != nil {
68-
return err
69-
}
70-
71-
body, _ = getBodyString(resp)
72-
return nil
73-
}
74-
75-
func jenkinsShouldBeUnlocked() error {
76-
if strings.Contains(body, "Unlock Jenkins") {
77-
return fmt.Errorf("expected %s not to contain 'Unlock Jenkins'", body)
78-
}
79-
return nil
80-
}
81-
82-
func iAccessPluginManagement() error {
83-
u := jenkinsHostUrl + "/pluginManager/api/xml?depth=1"
84-
pluginsResp, err := httpClient.Get(u)
85-
86-
if err != nil {
87-
return err
88-
}
89-
body, _ = getBodyString(pluginsResp)
90-
return nil
91-
}
92-
93-
func allThePluginsAreInstalled() error {
94-
if !strings.Contains(body, "<shortName>cucumber-reports</shortName>") {
95-
return fmt.Errorf("expected %s to contain 'cucumber-reports'", body)
96-
}
97-
return nil
98-
}
99-
100-
func getNewJenkinsCrumb() error {
101-
u := jenkinsHostUrl + "/crumbIssuer/api/json"
102-
resp, err := httpClient.Get(u)
103-
104-
if err != nil {
105-
return fmt.Errorf("expected response from crumbIssuer, got: %s", body)
106-
}
107-
108-
defer resp.Body.Close()
109-
110-
body_bytes, _ := ioutil.ReadAll(resp.Body)
111-
112-
if ! strings.Contains(body, `{"_class":"hudson.security.csrf.DefaultCrumbIssuer","crumb":`) {
113-
return fmt.Errorf("expected %s to contain '/logout' link", body)
114-
}
115-
116-
json.Unmarshal(body_bytes, &crumb)
117-
118-
return nil
119-
}
120-
121-
func iHaveLoggedIntoJenkins() error {
122-
123-
getNewJenkinsCrumb()
124-
125-
loginUrl := jenkinsHostUrl + "/j_acegi_security_check"
126-
jenkinsPassword := os.Getenv("JENKINS_PASSWORD")
127-
128-
resp, err := httpClient.PostForm(loginUrl,
129-
url.Values{"j_username": {"administrator"}, "j_password": {jenkinsPassword}, "Jenkins-Crumb": {crumb.Crumb}})
130-
131-
if err != nil {
132-
return fmt.Errorf("%s", err)
133-
}
134-
defer resp.Body.Close()
135-
136-
body, _ := ioutil.ReadAll(resp.Body)
137-
138-
if !strings.Contains(string(body), `<a href="/logout"><b>log out</b></a>`) {
139-
return fmt.Errorf("expected %s to contain '/logout' link", body)
140-
}
141-
142-
return nil
143-
}
144-
1457
func FeatureContext(s *godog.Suite) {
146-
s.Step(`^there is a jenkins install$`, thereIsAJenkinsInstall)
147-
148-
s.Step(`^I have logged into Jenkins$`, iHaveLoggedIntoJenkins)
149-
8+
s.Step(`^there is a Jenkins install$`, thereIsAJenkinsInstall)
1509
s.Step(`^I access the login screen$`, iAccessTheLoginScreen)
151-
s.Step(`^jenkins should be unlocked$`, jenkinsShouldBeUnlocked)
152-
10+
s.Step(`^Jenkins should be unlocked$`, jenkinsShouldBeUnlocked)
11+
s.Step(`^I login using invalid credentials$`, iLoginUsingInvalidCredentials)
12+
s.Step(`^I am not logged in$`, iAmNotLoggedIn)
13+
s.Step(`^I login using valid credentials$`, iLoginUsingValidCredentials)
14+
s.Step(`^I am logged in$`, iAmLoggedIn)
15315
s.Step(`^I access plugin management$`, iAccessPluginManagement)
15416
s.Step(`^all the plugins are installed$`, allThePluginsAreInstalled)
155-
}
17+
18+
s.BeforeScenario(func(interface{}) {
19+
createNewHttpClient()
20+
})
21+
}

0 commit comments

Comments
 (0)