github.com/billybanfield/evergreen@v0.0.0-20170525200750-eeee692790f7/thirdparty/jira_test.go (about)

     1  package thirdparty
     2  
     3  import (
     4  	"bytes"
     5  	"io/ioutil"
     6  	"net/http"
     7  	"testing"
     8  
     9  	"github.com/evergreen-ci/evergreen/testutil"
    10  	"github.com/pkg/errors"
    11  	. "github.com/smartystreets/goconvey/convey"
    12  )
    13  
    14  type stubHttp struct {
    15  	res *http.Response
    16  	err error
    17  }
    18  
    19  func (self stubHttp) doGet(url string, username string, password string) (*http.Response, error) {
    20  	return self.res, self.err
    21  }
    22  
    23  func (self stubHttp) doPost(url string, username string, password string, content interface{}) (*http.Response, error) {
    24  	return self.res, self.err
    25  }
    26  
    27  func (self stubHttp) doPut(url string, username string, password string, content interface{}) (*http.Response, error) {
    28  	return self.res, self.err
    29  }
    30  
    31  func TestJiraNetworkFail(t *testing.T) {
    32  	Convey("With a JIRA rest interface with broken network", t, func() {
    33  		stub := stubHttp{nil, errors.New("Generic network error")}
    34  
    35  		jira := JiraHandler{stub, testConfig.Jira.Host, testConfig.Jira.Username, testConfig.Jira.Password}
    36  
    37  		Convey("fetching tickets should return a non-nil err", func() {
    38  			ticket, err := jira.GetJIRATicket("BF-1")
    39  			So(ticket, ShouldBeNil)
    40  			So(err.Error(), ShouldEqual, "Generic network error")
    41  		})
    42  	})
    43  }
    44  
    45  func TestJiraUnauthorized(t *testing.T) {
    46  	Convey("With a JIRA rest interface that makes an unauthorized response", t, func() {
    47  		stub := stubHttp{&http.Response{}, nil}
    48  
    49  		stub.res.StatusCode = 401
    50  		stub.res.Status = "401 Unauthorized"
    51  		stub.res.Body = ioutil.NopCloser(&bytes.Buffer{})
    52  
    53  		jira := JiraHandler{stub, testConfig.Jira.Host, testConfig.Jira.Username, testConfig.Jira.Password}
    54  
    55  		Convey("fetching tickets should return 401 unauth error", func() {
    56  			ticket, err := jira.GetJIRATicket("BF-1")
    57  			So(ticket, ShouldBeNil)
    58  			So(err.Error(), ShouldEqual, "HTTP request returned unexpected status `401 Unauthorized`")
    59  		})
    60  	})
    61  }
    62  
    63  func TestJiraIntegration(t *testing.T) {
    64  	testutil.ConfigureIntegrationTest(t, testConfig, "TestJiraIntegration")
    65  	Convey("With a JIRA rest interface that makes a valid request", t, func() {
    66  		jira := JiraHandler{liveHttp{}, testConfig.Jira.Host, testConfig.Jira.Username, testConfig.Jira.Password}
    67  
    68  		Convey("the request for a ticket should return a valid ticket response", func() {
    69  			ticket, err := jira.GetJIRATicket("BF-1")
    70  			So(err, ShouldBeNil)
    71  			So(ticket.Key, ShouldEqual, "BF-1")
    72  			So(ticket.Fields.Project.Name, ShouldEqual, "Build Failures")
    73  		})
    74  	})
    75  }