-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_test.go
139 lines (126 loc) · 3.31 KB
/
client_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package healthcrm
import (
"context"
"encoding/json"
"net/http"
"net/url"
"testing"
"github.com/jarcoal/httpmock"
"github.com/savannahghi/authutils"
)
// MockAuthUtilsLib is a mock implementation of the authUtilsLib interface
type MockAuthUtilsLib struct{}
// Authenticate mocks implementation of authutil's library
func (m *MockAuthUtilsLib) Authenticate() (*authutils.OAUTHResponse, error) {
return &authutils.OAUTHResponse{
AccessToken: "mockAccessToken",
}, nil
}
func TestMakeRequest(t *testing.T) {
ctx := context.Background()
queryParam := url.Values{}
queryParam.Add("param1", "value1")
tests := []struct {
name string
method string
path string
queryParams url.Values
body interface{}
want int
}{
{
name: "Happy case: GET Request",
method: http.MethodGet,
path: "/v1/facilities/facilities/",
queryParams: queryParam,
body: &Facility{
Name: "Test Facility",
Description: "A test facility",
FacilityType: "HOSPITAL",
County: "Meru",
Country: "KE",
Address: "1200-Meru",
Coordinates: &Coordinates{
Latitude: "30.40338",
Longitude: "5.17403",
},
Contacts: []Contacts{
{
ContactType: "PHONE_NUMBER",
ContactValue: "+254788223223",
Role: "PRIMARY_CONTACT",
},
},
Identifiers: []Identifiers{
{
IdentifierType: "SLADE_CODE",
IdentifierValue: "3243",
ValidFrom: "2022-09-01",
ValidTo: "2023-09-01",
},
},
BusinessHours: []BusinessHours{},
},
want: http.StatusOK,
},
{
name: "Happy case: POST Request",
method: http.MethodPost,
path: "/v1/facilities/facilities/",
queryParams: nil,
body: &Facility{
Name: "Test Facility",
Description: "A test facility",
FacilityType: "HOSPITAL",
County: "Meru",
Country: "KE",
Address: "1200-Meru",
Coordinates: &Coordinates{
Latitude: "30.40338",
Longitude: "5.17403",
},
Contacts: []Contacts{
{
ContactType: "PHONE_NUMBER",
ContactValue: "+254788223223",
Role: "PRIMARY_CONTACT",
},
},
Identifiers: []Identifiers{
{
IdentifierType: "SLADE_CODE",
IdentifierValue: "3243",
ValidFrom: "2022-09-01",
ValidTo: "2023-09-01",
},
},
BusinessHours: []BusinessHours{},
},
want: http.StatusOK,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
httpmock.RegisterResponder(tt.method, tt.path, func(req *http.Request) (*http.Response, error) {
responseData := map[string]string{"message": "mockResponse"}
responseJSON, _ := json.Marshal(responseData)
return httpmock.NewStringResponse(tt.want, string(responseJSON)), nil
})
httpmock.Activate()
defer httpmock.DeactivateAndReset()
mockClient := &client{
authClient: &MockAuthUtilsLib{},
httpClient: &http.Client{},
}
response, err := mockClient.MakeRequest(ctx, tt.method, tt.path, tt.queryParams, tt.body)
if err != nil {
t.Errorf("Error making request: %v", err)
return
}
defer response.Body.Close()
if response.StatusCode != tt.want {
t.Errorf("Expected status code %d, got %d", tt.want, response.StatusCode)
}
})
}
}