github.com/companieshouse/lfp-pay-api@v0.0.0-20230203133422-0ca455cd79f9/handlers/create_payable_resource_test.go (about)

     1  package handlers
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"encoding/json"
     7  	"errors"
     8  	"net/http"
     9  	"net/http/httptest"
    10  	"os"
    11  	"testing"
    12  
    13  	"github.com/companieshouse/chs.go/authentication"
    14  	"github.com/companieshouse/lfp-pay-api-core/models"
    15  	"github.com/companieshouse/lfp-pay-api/config"
    16  	"github.com/companieshouse/lfp-pay-api/dao"
    17  	"github.com/companieshouse/lfp-pay-api/mocks"
    18  	"github.com/golang/mock/gomock"
    19  	"github.com/jarcoal/httpmock"
    20  	. "github.com/smartystreets/goconvey/convey"
    21  )
    22  
    23  func serveCreatePayableResourceHandler(body []byte, service dao.Service) *httptest.ResponseRecorder {
    24  	path := "/company/1000024/penalties/late-filing/payable"
    25  	req := httptest.NewRequest(http.MethodPost, path, bytes.NewReader(body))
    26  	res := httptest.NewRecorder()
    27  
    28  	handler := CreatePayableResourceHandler(service)
    29  	handler.ServeHTTP(res, req.WithContext(testContext()))
    30  
    31  	return res
    32  }
    33  
    34  func testContext() context.Context {
    35  	ctx := context.Background()
    36  	ctx = context.WithValue(ctx, authentication.ContextKeyUserDetails, authentication.AuthUserDetails{})
    37  	ctx = context.WithValue(ctx, config.CompanyNumber, "10000024")
    38  	return ctx
    39  }
    40  
    41  var e5Response = `
    42  {
    43    "page": {
    44      "size": 4,
    45      "totalElements": 4,
    46      "totalPages": 1,
    47      "number": 0
    48    },
    49    "data": [
    50      {
    51        "companyCode": "LP",
    52        "ledgerCode": "EW",
    53        "customerCode": "10000024",
    54        "transactionReference": "00378420",
    55        "transactionDate": "2017-11-28",
    56        "madeUpDate": "2017-02-28",
    57        "amount": 150,
    58        "outstandingAmount": 150,
    59        "isPaid": false,
    60        "transactionType": "1",
    61        "transactionSubType": "EU",
    62        "typeDescription": "Penalty Ltd Wel & Eng <=1m     LTDWA    ",
    63        "dueDate": "2017-12-12"
    64      }
    65    ]
    66  }
    67  `
    68  
    69  var e5ResponseMultipleTx = `
    70  {
    71    "page": {
    72      "size": 4,
    73      "totalElements": 4,
    74      "totalPages": 1,
    75      "number": 0
    76    },
    77    "data": [
    78      {
    79        "companyCode": "LP",
    80        "ledgerCode": "EW",
    81        "customerCode": "10000024",
    82        "transactionReference": "00378420",
    83        "transactionDate": "2017-11-28",
    84        "madeUpDate": "2017-02-28",
    85        "amount": 150,
    86        "outstandingAmount": 150,
    87        "isPaid": false,
    88        "transactionType": "1",
    89        "transactionSubType": "EU",
    90        "typeDescription": "Penalty Ltd Wel & Eng <=1m     LTDWA    ",
    91        "dueDate": "2017-12-12"
    92      },
    93      {
    94        "companyCode": "LP",
    95        "ledgerCode": "EW",
    96        "customerCode": "10000024",
    97        "transactionReference": "00378421",
    98        "transactionDate": "2017-11-28",
    99        "madeUpDate": "2017-02-28",
   100        "amount": 150,
   101        "outstandingAmount": 150,
   102        "isPaid": false,
   103        "transactionType": "1",
   104        "transactionSubType": "EU",
   105        "typeDescription": "Penalty Ltd Wel & Eng <=1m     LTDWA    ",
   106        "dueDate": "2017-12-12"
   107      }
   108    ]
   109  }
   110  `
   111  
   112  func TestUnitCreatePayableResourceHandler(t *testing.T) {
   113  	os.Chdir("..")
   114  	cfg, _ := config.Get()
   115  	cfg.E5APIURL = "https://e5"
   116  	cfg.E5Username = "SYSTEM"
   117  
   118  	url := "https://e5/arTransactions/10000024?ADV_userName=SYSTEM&companyCode=LP&fromDate=1990-01-01"
   119  
   120  	Convey("Must need at least one transaction", t, func() {
   121  		httpmock.Activate()
   122  		mockCtrl := gomock.NewController(t)
   123  		defer httpmock.DeactivateAndReset()
   124  		defer mockCtrl.Finish()
   125  
   126  		httpmock.RegisterResponder("GET", url, httpmock.NewStringResponder(200, e5Response))
   127  
   128  		body, _ := json.Marshal(&models.PayableRequest{})
   129  		res := serveCreatePayableResourceHandler(body, mocks.NewMockService(mockCtrl))
   130  
   131  		So(res.Code, ShouldEqual, http.StatusBadRequest)
   132  	})
   133  
   134  	Convey("Only allowed 1 transaction in a resource", t, func() {
   135  		httpmock.Activate()
   136  		mockCtrl := gomock.NewController(t)
   137  		defer httpmock.DeactivateAndReset()
   138  		defer mockCtrl.Finish()
   139  
   140  		httpmock.RegisterResponder("GET", url, httpmock.NewStringResponder(200, e5ResponseMultipleTx))
   141  		mockService := mocks.NewMockService(mockCtrl)
   142  
   143  		body, _ := json.Marshal(&models.PayableRequest{
   144  			CompanyNumber: "10000024",
   145  			CreatedBy:     authentication.AuthUserDetails{},
   146  			Transactions: []models.TransactionItem{
   147  				{TransactionID: "00378420", Amount: 150, MadeUpDate: "2017-02-28", Type: "penalty"},
   148  				{TransactionID: "00378421", Amount: 150, MadeUpDate: "2017-02-28", Type: "penalty"},
   149  			},
   150  		})
   151  
   152  		res := serveCreatePayableResourceHandler(body, mockService)
   153  
   154  		So(res.Code, ShouldEqual, http.StatusBadRequest)
   155  	})
   156  
   157  	Convey("internal server error when failing to create payable resource", t, func() {
   158  		httpmock.Activate()
   159  		mockCtrl := gomock.NewController(t)
   160  		defer httpmock.DeactivateAndReset()
   161  		defer mockCtrl.Finish()
   162  
   163  		httpmock.RegisterResponder("GET", url, httpmock.NewStringResponder(200, e5Response))
   164  		mockService := mocks.NewMockService(mockCtrl)
   165  
   166  		// expect the CreatePayableResource to be called once and return an error
   167  		mockService.EXPECT().CreatePayableResource(gomock.Any()).Return(errors.New("any error"))
   168  
   169  		body, _ := json.Marshal(&models.PayableRequest{
   170  			CompanyNumber: "10000024",
   171  			CreatedBy:     authentication.AuthUserDetails{},
   172  			Transactions: []models.TransactionItem{
   173  				{TransactionID: "00378420", Amount: 150, MadeUpDate: "2017-02-28", Type: "penalty"},
   174  			},
   175  		})
   176  
   177  		res := serveCreatePayableResourceHandler(body, mockService)
   178  
   179  		So(res.Code, ShouldEqual, http.StatusInternalServerError)
   180  	})
   181  
   182  	Convey("successfully creating a payable request", t, func() {
   183  		httpmock.Activate()
   184  		mockCtrl := gomock.NewController(t)
   185  		defer httpmock.DeactivateAndReset()
   186  		defer mockCtrl.Finish()
   187  
   188  		httpmock.RegisterResponder("GET", url, httpmock.NewStringResponder(200, e5Response))
   189  		mockService := mocks.NewMockService(mockCtrl)
   190  
   191  		// expect the CreatePayableResource to be called once and return without error
   192  		mockService.EXPECT().CreatePayableResource(gomock.Any()).Return(nil)
   193  
   194  		body, _ := json.Marshal(&models.PayableRequest{
   195  			CompanyNumber: "10000024",
   196  			CreatedBy:     authentication.AuthUserDetails{},
   197  			Transactions: []models.TransactionItem{
   198  				{TransactionID: "00378420", Amount: 150, MadeUpDate: "2017-02-28", Type: "penalty"},
   199  			},
   200  		})
   201  
   202  		res := serveCreatePayableResourceHandler(body, mockService)
   203  
   204  		So(res.Code, ShouldEqual, http.StatusCreated)
   205  		So(res.Header().Get("Content-Type"), ShouldEqual, "application/json")
   206  	})
   207  }