github.com/companieshouse/insolvency-api@v0.0.0-20231024103413-440c973d9e9b/service/alpha_key_service_test.go (about)

     1  package service
     2  
     3  import (
     4  	"net/http"
     5  	"testing"
     6  
     7  	"github.com/companieshouse/go-sdk-manager/config"
     8  	"github.com/companieshouse/insolvency-api/constants"
     9  	"github.com/jarcoal/httpmock"
    10  	. "github.com/smartystreets/goconvey/convey"
    11  )
    12  
    13  func alphaKeyResponse(companyName string) string {
    14  	return `
    15  	{
    16  		"sameAsAlphaKey": " ` + companyName + `",
    17  		"orderedAlphaKey": "COMPANYNAME",
    18  		"upperCaseName": "COMPANYNAME"
    19  	}`
    20  }
    21  func TestUnitCheckCompanyNameValid(t *testing.T) {
    22  
    23  	Convey("CheckCompanyInsolvencyValidFromAlphaKeyServiceAPI", t, func() {
    24  		httpmock.Activate()
    25  		defer httpmock.DeactivateAndReset()
    26  
    27  		Convey("Company name matches with company profile by alphakey", func() {
    28  			httpmock.RegisterResponder(http.MethodGet, "http://localhost:18103/alphakey?name=companyName", httpmock.NewStringResponder(http.StatusOK, alphaKeyResponse("COMPANYNAME")))
    29  			httpmock.RegisterResponder(http.MethodGet, "http://localhost:18103/alphakey?name=COMPANYNAME", httpmock.NewStringResponder(http.StatusOK, alphaKeyResponse("COMPANYNAME")))
    30  
    31  			request := incomingInsolvencyRequest("01234567", "companyName", constants.CVL.String())
    32  			err, statusCode := CheckCompanyNameAlphaKey("COMPANYNAME", request, &http.Request{})
    33  			So(err, ShouldBeNil)
    34  			So(statusCode, ShouldEqual, http.StatusOK)
    35  
    36  		})
    37  
    38  		Convey("Company name does not match with company profile by alphaley", func() {
    39  
    40  			httpmock.RegisterResponder(http.MethodGet, "http://localhost:18103/alphakey?name=companyName", httpmock.NewStringResponder(http.StatusOK, alphaKeyResponse("COMPANYNAME")))
    41  			httpmock.RegisterResponder(http.MethodGet, "http://localhost:18103/alphakey?name=ANOTHERNAME", httpmock.NewStringResponder(http.StatusOK, alphaKeyResponse("ANOTHERNAME")))
    42  
    43  			request := incomingInsolvencyRequest("01234567", "companyName", constants.CVL.String())
    44  			err, statusCode := CheckCompanyNameAlphaKey("ANOTHERNAME", request, &http.Request{})
    45  			So(err, ShouldNotBeNil)
    46  			So(err.Error(), ShouldContainSubstring, "company names do not match")
    47  			So(statusCode, ShouldEqual, http.StatusBadRequest)
    48  
    49  		})
    50  
    51  		Convey("Error contacting the alpha key service api", func() {
    52  			defer httpmock.Reset()
    53  			httpmock.RegisterResponder(http.MethodGet, "http://localhost:18103/alphakey?name=companyName", httpmock.NewStringResponder(http.StatusInternalServerError, ""))
    54  
    55  			request := incomingInsolvencyRequest("01234567", "companyName", constants.CVL.String())
    56  			err, statusCode := CheckCompanyNameAlphaKey("companyName", request, &http.Request{})
    57  			So(err, ShouldNotBeNil)
    58  			So(statusCode, ShouldEqual, http.StatusInternalServerError)
    59  			So(err.Error(), ShouldContainSubstring, `error communicating with alphakey service`)
    60  		})
    61  
    62  		Convey("alphakeyapi url present in environment variable returns no error", func() {
    63  			cfg, _ := config.Get()
    64  			cfg.ALPHAKEYAPIURL = "http://localhost:8003"
    65  			defer httpmock.Reset()
    66  			httpmock.RegisterResponder(http.MethodGet, "http://localhost:8003/alphakey?name=companyName", httpmock.NewStringResponder(http.StatusOK, alphaKeyResponse("COMPANYNAME")))
    67  
    68  			request := incomingInsolvencyRequest("01234567", "companyName", constants.CVL.String())
    69  			err, statusCode := CheckCompanyNameAlphaKey("companyName", request, &http.Request{})
    70  			So(err, ShouldBeNil)
    71  			So(statusCode, ShouldEqual, http.StatusOK)
    72  		})
    73  	})
    74  
    75  }