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

     1  package transformers
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/companieshouse/lfp-pay-api-core/models"
     9  
    10  	. "github.com/smartystreets/goconvey/convey"
    11  )
    12  
    13  func TestUnitPayableResourceRequestToDB(t *testing.T) {
    14  	Convey("reference number is generated", t, func() {
    15  		req := &models.PayableRequest{
    16  			Transactions: []models.TransactionItem{
    17  				{TransactionID: "123"},
    18  			},
    19  		}
    20  		dao := PayableResourceRequestToDB(req)
    21  
    22  		So(dao.Reference, ShouldHaveLength, 10)
    23  	})
    24  
    25  	Convey("self link is constructed correctly", t, func() {
    26  		req := &models.PayableRequest{
    27  			CompanyNumber: "00006400",
    28  			Transactions: []models.TransactionItem{
    29  				{TransactionID: "123"},
    30  			},
    31  		}
    32  		dao := PayableResourceRequestToDB(req)
    33  
    34  		// ensure a reference is generated for the next assertion
    35  		So(dao.Reference, ShouldHaveLength, 10)
    36  
    37  		expected := fmt.Sprintf("/company/%s/penalties/late-filing/payable/%s", req.CompanyNumber, dao.Reference)
    38  		So(dao.Data.Links.Self, ShouldContainSubstring, expected)
    39  		So(dao.Data.Links.ResumeJourney, ShouldEqual, "/late-filing-penalty/company/00006400/penalty/123/view-penalties")
    40  	})
    41  }
    42  
    43  func TestUnitPayableResourceDaoToCreatedResponse(t *testing.T) {
    44  	Convey("link to self is correct", t, func() {
    45  		dao := &models.PayableResourceDao{
    46  			Reference: "1234",
    47  			Data: models.PayableResourceDataDao{
    48  				Links: models.PayableResourceLinksDao{
    49  					Self:          "/foo",
    50  					ResumeJourney: "/company/12345678/penalty/123/lfp/view-penalties",
    51  				},
    52  				Transactions: map[string]models.TransactionDao{
    53  					"123": models.TransactionDao{Amount: 100, Type: "penalty", MadeUpDate: "2019-01-01"},
    54  				},
    55  			},
    56  		}
    57  
    58  		response := PayableResourceDaoToCreatedResponse(dao)
    59  
    60  		So(response.Links.Self, ShouldEqual, dao.Data.Links.Self)
    61  	})
    62  }
    63  
    64  func TestUnitPayableResourceDBToPayableResource(t *testing.T) {
    65  	Convey("field mappings are correct", t, func() {
    66  		t := time.Now().Truncate(time.Millisecond)
    67  		dao := &models.PayableResourceDao{
    68  			CompanyNumber: "12345678",
    69  			Reference:     "1234",
    70  			Data: models.PayableResourceDataDao{
    71  				Etag:      "qwertyetag1234",
    72  				CreatedAt: &t,
    73  				CreatedBy: models.CreatedByDao{
    74  					ID:       "uz3r_1d",
    75  					Email:    "test@user.com",
    76  					Forename: "some",
    77  					Surname:  "body",
    78  				},
    79  				Links: models.PayableResourceLinksDao{
    80  					Self:          "/foo",
    81  					Payment:       "/foo/pay",
    82  					ResumeJourney: "/company/12345678/penalty/123/lfp/view-penalties",
    83  				},
    84  				Payment: models.PaymentDao{
    85  					Amount:    "100",
    86  					Status:    "pending",
    87  					Reference: "payref",
    88  					PaidAt:    &t,
    89  				},
    90  				Transactions: map[string]models.TransactionDao{
    91  					"123": models.TransactionDao{
    92  						Amount:     100,
    93  						Type:       "penalty",
    94  						MadeUpDate: "2019-01-01",
    95  					},
    96  				},
    97  			},
    98  		}
    99  
   100  		response := PayableResourceDBToRequest(dao)
   101  
   102  		So(response.CompanyNumber, ShouldEqual, dao.CompanyNumber)
   103  		So(response.Reference, ShouldEqual, dao.Reference)
   104  		So(response.Etag, ShouldEqual, dao.Data.Etag)
   105  		So(response.CreatedAt, ShouldEqual, dao.Data.CreatedAt)
   106  		So(response.CreatedBy.ID, ShouldEqual, dao.Data.CreatedBy.ID)
   107  		So(response.CreatedBy.Email, ShouldEqual, dao.Data.CreatedBy.Email)
   108  		So(response.CreatedBy.Forename, ShouldEqual, dao.Data.CreatedBy.Forename)
   109  		So(response.CreatedBy.Surname, ShouldEqual, dao.Data.CreatedBy.Surname)
   110  		So(response.Links.Self, ShouldEqual, dao.Data.Links.Self)
   111  		So(response.Links.Payment, ShouldEqual, dao.Data.Links.Payment)
   112  		So(response.Links.ResumeJourney, ShouldEqual, dao.Data.Links.ResumeJourney)
   113  		So(response.Payment.Amount, ShouldEqual, dao.Data.Payment.Amount)
   114  		So(response.Payment.Status, ShouldEqual, dao.Data.Payment.Status)
   115  		So(response.Payment.Reference, ShouldEqual, dao.Data.Payment.Reference)
   116  		So(response.Payment.PaidAt, ShouldEqual, dao.Data.Payment.PaidAt)
   117  		So(len(response.Transactions), ShouldEqual, 1)
   118  		So(response.Transactions[0].Amount, ShouldEqual, dao.Data.Transactions["123"].Amount)
   119  		So(response.Transactions[0].Type, ShouldEqual, dao.Data.Transactions["123"].Type)
   120  		So(response.Transactions[0].MadeUpDate, ShouldEqual, dao.Data.Transactions["123"].MadeUpDate)
   121  	})
   122  }
   123  
   124  func TestUnitPayableResourceToPaymentDetails(t *testing.T) {
   125  	Convey("field mappings are correct from payable resource to payment details", t, func() {
   126  		t := time.Now().Truncate(time.Millisecond)
   127  		payable := &models.PayableResource{
   128  			CompanyNumber: "12345678",
   129  			Reference:     "1234",
   130  			Etag:          "qwertyetag1234",
   131  			CreatedAt:     &t,
   132  			CreatedBy: models.CreatedBy{
   133  				ID:       "uz3r_1d",
   134  				Email:    "test@user.com",
   135  				Forename: "some",
   136  				Surname:  "body",
   137  			},
   138  			Links: models.PayableResourceLinks{
   139  				Self:    "/foo",
   140  				Payment: "/foo/pay",
   141  			},
   142  			Payment: models.Payment{
   143  				Amount:    "100",
   144  				Status:    "pending",
   145  				Reference: "payref",
   146  				PaidAt:    &t,
   147  			},
   148  			Transactions: []models.TransactionItem{
   149  				models.TransactionItem{
   150  					Amount:     100,
   151  					Type:       "penalty",
   152  					MadeUpDate: "2019-01-01",
   153  				},
   154  			},
   155  		}
   156  
   157  		response := PayableResourceToPaymentDetails(payable)
   158  
   159  		So(response, ShouldNotBeNil)
   160  		So(response.Description, ShouldEqual, "Late Filing Penalty")
   161  		So(response.Kind, ShouldEqual, "payment-details#payment-details")
   162  		So(response.PaidAt, ShouldEqual, payable.Payment.PaidAt)
   163  		So(response.PaymentReference, ShouldEqual, payable.Payment.Reference)
   164  		So(response.Links.Self, ShouldEqual, payable.Links.Payment)
   165  		So(response.Links.Resource, ShouldEqual, payable.Links.Self)
   166  		So(response.Status, ShouldEqual, payable.Payment.Status)
   167  		So(response.CompanyNumber, ShouldEqual, payable.CompanyNumber)
   168  		So(len(response.Items), ShouldEqual, 1)
   169  		So(response.Items[0].Amount, ShouldEqual, fmt.Sprintf("%g", payable.Transactions[0].Amount))
   170  		So(response.Items[0].AvailablePaymentMethods, ShouldResemble, []string{"credit-card"})
   171  		So(response.Items[0].ClassOfPayment, ShouldResemble, []string{"penalty"})
   172  		So(response.Items[0].Description, ShouldEqual, "Late Filing Penalty")
   173  		So(response.Items[0].DescriptionIdentifier, ShouldEqual, "late-filing-penalty")
   174  		So(response.Items[0].Kind, ShouldEqual, "cost#cost")
   175  		So(response.Items[0].ResourceKind, ShouldEqual, "late-filing-penalty#late-filing-penalty")
   176  		So(response.Items[0].ProductType, ShouldEqual, "late-filing-penalty")
   177  	})
   178  }