11package main
22
33import (
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-
1457func 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