github.com/companieshouse/insolvency-api@v0.0.0-20231024103413-440c973d9e9b/service/company_service_test.go (about) 1 package service 2 3 import ( 4 "encoding/json" 5 "net/http" 6 "testing" 7 8 "github.com/companieshouse/api-sdk-go/companieshouseapi" 9 "github.com/companieshouse/insolvency-api/constants" 10 "github.com/companieshouse/insolvency-api/models" 11 "github.com/jarcoal/httpmock" 12 . "github.com/smartystreets/goconvey/convey" 13 ) 14 15 func incomingInsolvencyRequest(companyNumber string, companyName string, caseType string) *models.InsolvencyRequest { 16 return &models.InsolvencyRequest{ 17 CompanyNumber: companyNumber, 18 CompanyName: companyName, 19 CaseType: caseType, 20 } 21 } 22 23 func companyProfileResponse(jurisdiction string, companyStatus string, companyType string) string { 24 return ` 25 { 26 "company_name": "COMPANYNAME", 27 "company_number": "01234567", 28 "jurisdiction": "` + jurisdiction + `", 29 "company_status": "` + companyStatus + `", 30 "date_of_creation": "2000-06-26 00:00:00.000Z", 31 "type": "` + companyType + `", 32 "registered_office_address" : { 33 "postal_code" : "CF14 3UZ", 34 "address_line_2" : "Cardiff", 35 "address_line_1" : "1 Crown Way" 36 } 37 } 38 ` 39 } 40 41 var apiURL = "https://api.companieshouse.gov.uk" 42 43 func TestUnitCheckCompanyInsolvencyValid(t *testing.T) { 44 45 Convey("CheckCompanyInsolvencyValidFromCompanyProfileAPI", t, func() { 46 var companyProfile *companieshouseapi.CompanyProfile 47 httpmock.Activate() 48 defer httpmock.DeactivateAndReset() 49 50 Convey("Company cannot be found on company profile api", func() { 51 defer httpmock.Reset() 52 httpmock.RegisterResponder(http.MethodGet, apiURL+"/company/01234567", httpmock.NewStringResponder(http.StatusNotFound, "Message: Company not found")) 53 54 request := incomingInsolvencyRequest("01234567", "companyName", constants.CVL.String()) 55 err, statusCode, companyProfile := CheckCompanyExists(request, &http.Request{}) 56 So(err, ShouldNotBeNil) 57 So(statusCode, ShouldEqual, http.StatusNotFound) 58 So(err.Error(), ShouldEqual, `company not found`) 59 So(companyProfile, ShouldBeNil) 60 }) 61 62 Convey("Error contacting the company profile api", func() { 63 defer httpmock.Reset() 64 httpmock.RegisterResponder(http.MethodGet, apiURL+"/company/01234567", httpmock.NewStringResponder(http.StatusTeapot, "")) 65 66 request := incomingInsolvencyRequest("01234567", "companyName", constants.CVL.String()) 67 err, statusCode, companyProfile := CheckCompanyExists(request, &http.Request{}) 68 So(err, ShouldNotBeNil) 69 So(statusCode, ShouldEqual, http.StatusTeapot) 70 So(err.Error(), ShouldEqual, `error communicating with the company profile api`) 71 So(companyProfile, ShouldBeNil) 72 }) 73 74 Convey("Jurisdiction of company is not allowed to create insolvency case", func() { 75 76 // var companyProfile *companieshouseapi.CompanyProfile 77 json.Unmarshal([]byte(companyProfileResponse("scotland", "active", "private-shares-exemption-30")), &companyProfile) 78 79 err := CheckCompanyDetailsAreValid(companyProfile) 80 So(err, ShouldNotBeNil) 81 So(err.Error(), ShouldEqual, `jurisdiction [scotland] not permitted`) 82 }) 83 84 Convey("Company status is not allowed to create insolvency case", func() { 85 json.Unmarshal([]byte(companyProfileResponse("england-wales", "dissolved", "private-shares-exemption-30")), &companyProfile) 86 87 err := CheckCompanyDetailsAreValid(companyProfile) 88 So(err, ShouldNotBeNil) 89 So(err.Error(), ShouldEqual, `company status [dissolved] not permitted`) 90 }) 91 92 Convey("Company type is not allowed to create insolvency case", func() { 93 json.Unmarshal([]byte(companyProfileResponse("england-wales", "active", "converted-or-closed")), &companyProfile) 94 95 err := CheckCompanyDetailsAreValid(companyProfile) 96 So(err, ShouldNotBeNil) 97 So(err.Error(), ShouldEqual, `company type [converted-or-closed] not permitted`) 98 }) 99 100 Convey("Company is allowed to start insolvency case", func() { 101 json.Unmarshal([]byte(companyProfileResponse("england-wales", "active", "private-shares-exemption-30")), &companyProfile) 102 103 err := CheckCompanyDetailsAreValid(companyProfile) 104 So(err, ShouldBeNil) 105 }) 106 }) 107 } 108 109 func TestUnitGetCompanyIncorporatedOn(t *testing.T) { 110 Convey("CheckCompanyInsolvencyValidFromCompanyProfileAPI", t, func() { 111 112 httpmock.Activate() 113 defer httpmock.DeactivateAndReset() 114 companyNumber := "01234567" 115 116 Convey("Company cannot be found on company profile api", func() { 117 defer httpmock.Reset() 118 httpmock.RegisterResponder(http.MethodGet, apiURL+"/company/01234567", httpmock.NewStringResponder(http.StatusNotFound, "Message: Company not found")) 119 120 date, err := GetCompanyIncorporatedOn(companyNumber, &http.Request{}) 121 So(date, ShouldBeEmpty) 122 So(err.Error(), ShouldEqual, `company not found`) 123 }) 124 125 Convey("Error contacting the company profile api", func() { 126 defer httpmock.Reset() 127 httpmock.RegisterResponder(http.MethodGet, apiURL+"/company/01234567", httpmock.NewStringResponder(http.StatusTeapot, "")) 128 129 date, err := GetCompanyIncorporatedOn(companyNumber, &http.Request{}) 130 So(date, ShouldBeEmpty) 131 So(err.Error(), ShouldEqual, `error communicating with the company profile api`) 132 }) 133 134 Convey("Successfully retrieve incorporated on date", func() { 135 defer httpmock.Reset() 136 httpmock.RegisterResponder(http.MethodGet, apiURL+"/company/01234567", httpmock.NewStringResponder(http.StatusOK, companyProfileResponse("england-wales", "active", "private-shares-exemption-30"))) 137 138 date, err := GetCompanyIncorporatedOn(companyNumber, &http.Request{}) 139 So(date, ShouldEqual, "2000-06-26 00:00:00.000Z") 140 So(err, ShouldBeNil) 141 }) 142 }) 143 }