Files
cpone_midleware/internal/http/hospitals_test.go

51 lines
1.9 KiB
Go

package http
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
"primaya-api/cpone-middleware/internal/databaseconfig"
)
type fakeHospitalStore struct {
fakeDatabaseSettingsStore
settings []databaseconfig.PublicSetting
}
func (f *fakeHospitalStore) List() []databaseconfig.PublicSetting { return f.settings }
func TestListHospitalsOnlyReturnsActiveWithoutDatabaseCredentials(t *testing.T) {
store := &fakeHospitalStore{settings: []databaseconfig.PublicSetting{
{RSCode: "AWALBROS_DEV", Name: "RS Dev Awalbros", Host: "secret-host", Username: "secret-user", Active: true},
{RSCode: "INACTIVE_RS", Name: "RS Tidak Aktif", Host: "inactive-host", Active: false},
}}
handler := NewDatabaseSettingsHandler(store)
recorder := httptest.NewRecorder()
handler.ListHospitals(recorder, httptest.NewRequest(http.MethodGet, "/api/cpone/hospitals", nil))
if recorder.Code != http.StatusOK {
t.Fatalf("status=%d body=%s", recorder.Code, recorder.Body.String())
}
for _, expected := range []string{`"kode_rs":"AWALBROS_DEV"`, `"nama":"RS Dev Awalbros"`, `"status":true`} {
if !strings.Contains(recorder.Body.String(), expected) {
t.Fatalf("response missing %s: %s", expected, recorder.Body.String())
}
}
for _, forbidden := range []string{"INACTIVE_RS", "RS Tidak Aktif", "secret-host", "secret-user", `"host"`, `"username"`, `"password"`} {
if strings.Contains(recorder.Body.String(), forbidden) {
t.Fatalf("response contains %q: %s", forbidden, recorder.Body.String())
}
}
}
func TestListHospitalsReturnsEmptyArray(t *testing.T) {
handler := NewDatabaseSettingsHandler(&fakeHospitalStore{})
recorder := httptest.NewRecorder()
handler.ListHospitals(recorder, httptest.NewRequest(http.MethodGet, "/api/cpone/hospitals", nil))
if recorder.Body.String() != `{"success":true,"message":"ok","data":[]}`+"\n" {
t.Fatalf("body=%s", recorder.Body.String())
}
}