-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_test.go
205 lines (184 loc) · 5.56 KB
/
server_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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
type Address struct {
Country string `json:"country"`
Landmark string `json:"landmark"`
City string `json:"city"`
State string `json:"state"`
}
type RegisterRequest struct {
Name string `json:"name"`
Availability string `json:"availability"`
TotalFacilities int `json:"total_facilities"`
TotalMbbsDoc int `json:"total_mbbs_doc"`
TotalWorker int `json:"total_worker"`
NoOfBeds int `json:"no_of_beds"`
Email string `json:"email"`
AppointmentFee int `json:"appointment_fee"`
About string `json:"about"`
Password string `json:"password"`
Address Address `json:"address"`
}
type HealthcareDetails struct {
HealthcareID string `json:"healthcare_id"`
HealthcareLicense string `json:"healthcare_license"`
}
type RegisterResponse struct {
Status string `json:"status"`
HealthcareDetails HealthcareDetails `json:"healthcare_details"`
}
func RegisterHandler(w http.ResponseWriter, r *http.Request) {
var req RegisterRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
response := RegisterResponse{
Status: "Successfully Created",
HealthcareDetails: HealthcareDetails{
HealthcareID: "HCID12345",
HealthcareLicense: "HCID123",
},
}
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(response)
}
func TestRegisterEndpoint(t *testing.T) {
tests := []struct {
name string
request RegisterRequest
expectedStatus int
validateResponse func(*testing.T, []byte)
}{
{
name: "Successful Registration",
request: RegisterRequest{
Name: "Test Hospital",
Availability: "Yes",
TotalFacilities: 200,
TotalMbbsDoc: 58,
TotalWorker: 400,
NoOfBeds: 200,
Email: "[email protected]",
AppointmentFee: 300,
About: "Test Hospital Description",
Password: "11",
Address: Address{
Country: "India",
Landmark: "Test Landmark",
City: "Test City",
State: "Test State",
},
},
expectedStatus: http.StatusCreated,
validateResponse: func(t *testing.T, body []byte) {
var response RegisterResponse
err := json.Unmarshal(body, &response)
fmt.Println("RESPONSE***********", response)
assert.NoError(t, err)
assert.Equal(t, "Successfully Created", response.Status)
assert.NotEmpty(t, response.HealthcareDetails.HealthcareID)
assert.NotEmpty(t, response.HealthcareDetails.HealthcareLicense)
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
jsonBody, _ := json.Marshal(tt.request)
req := httptest.NewRequest("POST", "http://localhost:3000/api/v1/healthcareauth/register", bytes.NewBuffer(jsonBody))
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
RegisterHandler(w, req) // Call the handler function
assert.Equal(t, tt.expectedStatus, w.Code)
if tt.validateResponse != nil {
tt.validateResponse(t, w.Body.Bytes())
}
})
}
}
type LoginRequest struct {
HealthcareID string `json:"healthcare_id"`
HealthcareLicense string `json:"healthcare_license"`
Password string `json:"password"`
}
type LoginResponse struct {
Token string `json:"token"`
ExpiresIn string `json:"Expires In"`
HealthcareId string `json:"healthcare_id"`
HealthcareName string `json:"healthcare_name"`
}
func LoginHandler(w http.ResponseWriter, r *http.Request) {
var req LoginRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if req.HealthcareID == "HCID123" && req.HealthcareLicense == "HCID123" && req.Password == "11" {
response := LoginResponse{
Token: "tokengoeshere",
ExpiresIn: "5d",
}
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(response)
} else {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
}
}
func TestLoginEndpoint(t *testing.T) {
tests := []struct {
name string
request LoginRequest
expectedStatus int
validateResponse func(*testing.T, []byte)
}{
{
name: "Successful Login",
request: LoginRequest{
HealthcareID: "HCID123",
HealthcareLicense: "LICENSE123",
Password: "11",
},
expectedStatus: http.StatusOK,
validateResponse: func(t *testing.T, body []byte) {
var response LoginResponse
err := json.Unmarshal(body, &response)
assert.NoError(t, err)
assert.NotEmpty(t, response.Token)
assert.Equal(t, "5d", response.ExpiresIn)
},
},
{
name: "Failed Login - Invalid Credentials",
request: LoginRequest{
HealthcareID: "INVALID_ID",
HealthcareLicense: "INVALID_LICENSE",
Password: "wrong_password",
},
expectedStatus: http.StatusUnauthorized,
validateResponse: func(t *testing.T, body []byte) {
assert.Empty(t, body) // Expecting no body on unauthorized
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
jsonBody, _ := json.Marshal(tt.request)
req := httptest.NewRequest("POST", "http://localhost:3000/api/v1/healthcareauth/login", bytes.NewBuffer(jsonBody))
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
LoginHandler(w, req)
assert.Equal(t, tt.expectedStatus, w.Code)
if tt.validateResponse != nil {
tt.validateResponse(t, w.Body.Bytes())
}
})
}
}