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

     1  package handlers
     2  
     3  import (
     4  	"net/http"
     5  	"net/http/httptest"
     6  	"testing"
     7  
     8  	"github.com/companieshouse/insolvency-api/config"
     9  	mock_dao "github.com/companieshouse/insolvency-api/mocks"
    10  	"github.com/companieshouse/insolvency-api/utils"
    11  	"github.com/golang/mock/gomock"
    12  	"github.com/gorilla/mux"
    13  	. "github.com/smartystreets/goconvey/convey"
    14  )
    15  
    16  func setupTestRouter(t *testing.T) *mux.Router {
    17  	router := mux.NewRouter()
    18  	mockCtrl := gomock.NewController(t)
    19  	defer mockCtrl.Finish()
    20  	mockService := mock_dao.NewMockService(mockCtrl)
    21  	helperService := utils.NewHelperService()
    22  	Register(router, mockService, helperService)
    23  	return router
    24  }
    25  
    26  func TestUnitRegisterRoutes(t *testing.T) {
    27  	// Certain routes are now disabled when ENABLE_NON_LIVE_ROUTE_HANDLERS is unset or false
    28  	cfg, _ := config.Get()
    29  
    30  	Convey("Register routes: ENABLE_NON_LIVE_ROUTE_HANDLERS unset or false", t, func() {
    31  		router := setupTestRouter(t)
    32  
    33  		So(router.GetRoute("healthcheck"), ShouldNotBeNil)
    34  
    35  		So(router.GetRoute("createInsolvencyResource"), ShouldNotBeNil)
    36  		So(router.GetRoute("getValidationStatus"), ShouldNotBeNil)
    37  		So(router.GetRoute("getFilings"), ShouldNotBeNil)
    38  
    39  		So(router.GetRoute("createPractitionersResource"), ShouldNotBeNil)
    40  		So(router.GetRoute("getPractitionerResources"), ShouldNotBeNil)
    41  		So(router.GetRoute("getPractitionerResource"), ShouldNotBeNil)
    42  		So(router.GetRoute("deletePractitioner"), ShouldNotBeNil)
    43  
    44  		So(router.GetRoute("appointPractitioner"), ShouldNotBeNil)
    45  		So(router.GetRoute("getPractitionerAppointment"), ShouldNotBeNil)
    46  		So(router.GetRoute("deletePractitionerAppointment"), ShouldNotBeNil)
    47  
    48  		So(router.GetRoute("submitAttachment"), ShouldNotBeNil)
    49  		So(router.GetRoute("getAttachmentDetails"), ShouldNotBeNil)
    50  		So(router.GetRoute("downloadAttachment"), ShouldNotBeNil)
    51  		So(router.GetRoute("deleteAttachment"), ShouldNotBeNil)
    52  
    53  		So(router.GetRoute("createResolution"), ShouldNotBeNil)
    54  		So(router.GetRoute("getResolution"), ShouldNotBeNil)
    55  		So(router.GetRoute("deleteResolution"), ShouldNotBeNil)
    56  
    57  		So(router.GetRoute("createStatementOfAffairs"), ShouldNotBeNil)
    58  		So(router.GetRoute("getStatementOfAffairs"), ShouldNotBeNil)
    59  		So(router.GetRoute("deleteStatementOfAffairs"), ShouldNotBeNil)
    60  
    61  		So(router.GetRoute("createProgressReport"), ShouldNotBeNil)
    62  		So(router.GetRoute("getProgressReport"), ShouldNotBeNil)
    63  	})
    64  
    65  	// Simulate ENABLE_NON_LIVE_ROUTE_HANDLERS feature toggle being enabled
    66  	cfg.EnableNonLiveRouteHandlers = true
    67  	Convey("Register routes: ENABLE_NON_LIVE_ROUTE_HANDLERS is set as true", t, func() {
    68  		router := setupTestRouter(t)
    69  
    70  		So(router.GetRoute("healthcheck"), ShouldNotBeNil)
    71  
    72  		So(router.GetRoute("createInsolvencyResource"), ShouldNotBeNil)
    73  		So(router.GetRoute("getValidationStatus"), ShouldNotBeNil)
    74  		So(router.GetRoute("getFilings"), ShouldNotBeNil)
    75  
    76  		So(router.GetRoute("createPractitionersResource"), ShouldNotBeNil)
    77  		So(router.GetRoute("getPractitionerResources"), ShouldNotBeNil)
    78  		So(router.GetRoute("getPractitionerResource"), ShouldNotBeNil)
    79  		So(router.GetRoute("deletePractitioner"), ShouldNotBeNil)
    80  
    81  		So(router.GetRoute("appointPractitioner"), ShouldNotBeNil)
    82  		So(router.GetRoute("getPractitionerAppointment"), ShouldNotBeNil)
    83  		So(router.GetRoute("deletePractitionerAppointment"), ShouldNotBeNil)
    84  
    85  		So(router.GetRoute("submitAttachment"), ShouldNotBeNil)
    86  		So(router.GetRoute("getAttachmentDetails"), ShouldNotBeNil)
    87  		So(router.GetRoute("downloadAttachment"), ShouldNotBeNil)
    88  		So(router.GetRoute("deleteAttachment"), ShouldNotBeNil)
    89  
    90  		So(router.GetRoute("createStatementOfAffairs"), ShouldNotBeNil)
    91  		So(router.GetRoute("getStatementOfAffairs"), ShouldNotBeNil)
    92  		So(router.GetRoute("deleteStatementOfAffairs"), ShouldNotBeNil)
    93  
    94  		So(router.GetRoute("createResolution"), ShouldNotBeNil)
    95  		So(router.GetRoute("getResolution"), ShouldNotBeNil)
    96  		So(router.GetRoute("deleteResolution"), ShouldNotBeNil)
    97  
    98  		So(router.GetRoute("createStatementOfAffairs"), ShouldNotBeNil)
    99  		So(router.GetRoute("getStatementOfAffairs"), ShouldNotBeNil)
   100  		So(router.GetRoute("deleteStatementOfAffairs"), ShouldNotBeNil)
   101  
   102  		So(router.GetRoute("createProgressReport"), ShouldNotBeNil)
   103  		So(router.GetRoute("getProgressReport"), ShouldNotBeNil)
   104  	})
   105  }
   106  
   107  func TestUnitHealthCheck(t *testing.T) {
   108  	Convey("Healthcheck", t, func() {
   109  		w := httptest.ResponseRecorder{}
   110  		healthCheck(&w, nil)
   111  		So(w.Code, ShouldEqual, http.StatusOK)
   112  	})
   113  }
   114  
   115  func TestUnitUrlParameterValidation(t *testing.T) {
   116  	router := setupTestRouter(t)
   117  	matchInfo := &mux.RouteMatch{}
   118  	Convey("Invalid Transaction ID matched to route", t, func() {
   119  		req, _ := http.NewRequest("GET", "/transactions/x/insolvency/validation-status", nil)
   120  		matched := router.Match(req, matchInfo)
   121  		So(matched, ShouldEqual, false)
   122  		So(matchInfo.MatchErr, ShouldEqual, mux.ErrNotFound)
   123  	})
   124  	Convey("Valid Transaction ID matched to route", t, func() {
   125  		req, _ := http.NewRequest("GET", "/transactions/068925-439616-777491/insolvency/validation-status", nil)
   126  		matched := router.Match(req, matchInfo)
   127  		So(matched, ShouldEqual, true)
   128  		So(matchInfo.MatchErr, ShouldBeNil)
   129  	})
   130  	Convey("Invalid Attachment ID matched to route", t, func() {
   131  		req, _ := http.NewRequest("GET", "/transactions/068925-439616-777491/insolvency/attachments/x", nil)
   132  		matched := router.Match(req, matchInfo)
   133  		So(matched, ShouldEqual, false)
   134  		So(matchInfo.MatchErr, ShouldEqual, mux.ErrNotFound)
   135  	})
   136  	Convey("Valid Attachment ID matched to route", t, func() {
   137  		req, _ := http.NewRequest("GET", "/transactions/068925-439616-777491/insolvency/attachments/36bd074f-96a8-46d7-ad6b-29dd67452f0a", nil)
   138  		matched := router.Match(req, matchInfo)
   139  		So(matched, ShouldEqual, true)
   140  		So(matchInfo.MatchErr, ShouldBeNil)
   141  	})
   142  }