51 lines
2.3 KiB
Go
51 lines
2.3 KiB
Go
package http
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"primaya-api/cpone-middleware/internal/repository"
|
|
)
|
|
|
|
func TestAddLabPackageValidPayload(t *testing.T) {
|
|
repo := &fakeLabRegistrationRepository{outcome: repository.LabRegistrationOutcome{
|
|
HTTPStatus: 201, Success: true, Status: "package_added",
|
|
Message: "Paket dispenser berhasil ditambahkan ke registrasi laboratorium",
|
|
}}
|
|
handler := Handler{labRepo: repo}
|
|
request := httptest.NewRequest(http.MethodPost, "/packages", strings.NewReader(`{"paket_id":"disp-260800011","cito":true,"markup_cito":"10","catatan":" test ","channel":"cpone"}`))
|
|
request.SetPathValue("regId", "lab-2608210003")
|
|
recorder := httptest.NewRecorder()
|
|
handler.AddLabPackage(recorder, request)
|
|
if recorder.Code != http.StatusCreated {
|
|
t.Fatalf("status=%d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
if repo.regID != "LAB-2608210003" || repo.packageParams.PackageID != "DISP-260800011" || !repo.packageParams.Cito || repo.packageParams.MarkupCito != 10 || repo.packageParams.Notes != "test" || repo.packageParams.Channel != "CPONE" {
|
|
t.Fatalf("unexpected repository call: reg=%q params=%+v", repo.regID, repo.packageParams)
|
|
}
|
|
}
|
|
|
|
func TestAddLabPackageValidation(t *testing.T) {
|
|
tests := []struct{ name, regID, body, message string }{
|
|
{"invalid registration", "LAB/1", `{"paket_id":"DISP-1"}`, "Nomor registrasi laboratorium hanya boleh berisi huruf, angka, dan tanda hubung"},
|
|
{"missing package", "LAB-1", `{}`, "Paket ID wajib diisi"},
|
|
{"invalid package", "LAB-1", `{"paket_id":"DISP/1"}`, "Paket ID hanya boleh berisi huruf, angka, dan tanda hubung"},
|
|
{"invalid cito", "LAB-1", `{"paket_id":"DISP-1","cito":"true"}`, "The cito field must be true or false."},
|
|
{"invalid markup", "LAB-1", `{"paket_id":"DISP-1","markup_cito":101}`, "The markup cito must be between 0 and 100."},
|
|
}
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
handler := Handler{labRepo: &fakeLabRegistrationRepository{}}
|
|
request := httptest.NewRequest(http.MethodPost, "/packages", strings.NewReader(test.body))
|
|
request.SetPathValue("regId", test.regID)
|
|
recorder := httptest.NewRecorder()
|
|
handler.AddLabPackage(recorder, request)
|
|
if recorder.Code != 422 || !strings.Contains(recorder.Body.String(), test.message) {
|
|
t.Fatalf("status=%d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
})
|
|
}
|
|
}
|