-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathservice_test.go
More file actions
150 lines (124 loc) · 5.41 KB
/
Copy pathservice_test.go
File metadata and controls
150 lines (124 loc) · 5.41 KB
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
package clinics_test
import (
"context"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
clinic "github.com/tidepool-org/clinic/client"
"go.mongodb.org/mongo-driver/bson/primitive"
authTest "github.com/tidepool-org/platform/auth/test"
"github.com/tidepool-org/platform/clinics"
summaryTest "github.com/tidepool-org/platform/summary/test"
userTest "github.com/tidepool-org/platform/user/test"
)
var _ = Describe("Client", func() {
var server *httptest.Server
var requestPath string
var requestBody []byte
var responseStatusCode int
var client clinics.Client
var patientID string
BeforeEach(func() {
patientID = userTest.RandomUserID()
requestPath = ""
requestBody = nil
server = httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
requestPath = req.URL.Path
var err error
requestBody, err = io.ReadAll(req.Body)
Expect(err).ToNot(HaveOccurred())
res.WriteHeader(responseStatusCode)
}))
GinkgoT().Setenv("TIDEPOOL_CLINIC_CLIENT_ADDRESS", server.URL)
externalAccessor := authTest.NewExternalAccessor()
externalAccessor.ServerSessionTokenOutputs = []authTest.ServerSessionTokenOutput{{Token: authTest.NewSessionToken()}}
var err error
client, err = clinics.NewClient(externalAccessor)
Expect(err).ToNot(HaveOccurred())
})
AfterEach(func() {
server.Close()
})
Context("SyncEHRDataForPatient", func() {
BeforeEach(func() {
responseStatusCode = http.StatusAccepted
})
It("requests a synchronization for the patient", func() {
Expect(client.SyncEHRDataForPatient(context.Background(), patientID)).To(Succeed())
Expect(requestPath).To(Equal("/v1/patients/" + patientID + "/ehr/sync"))
})
// The clinic service reports a patient with no active subscription to any clinic enabled for an
// electronic health record as not found. Most users are not such a patient, so reporting that as
// a failure would fail the work of nearly every user.
It("returns no error when the patient has no active subscription", func() {
responseStatusCode = http.StatusNotFound
Expect(client.SyncEHRDataForPatient(context.Background(), patientID)).To(Succeed())
})
It("returns an error when the clinic service reports an unexpected status", func() {
responseStatusCode = http.StatusInternalServerError
err := client.SyncEHRDataForPatient(context.Background(), patientID)
Expect(err).To(MatchError(ContainSubstring("unexpected response status code 500")))
})
})
Context("UpdatePatientSummary", func() {
var patientSummary *clinic.PatientSummaryV1
BeforeEach(func() {
responseStatusCode = http.StatusOK
cgm := summaryTest.RandomCGMSummary(patientID)
cgm.ID = primitive.NewObjectID()
patientSummary = clinics.NewPatientSummary(cgm, nil)
})
It("updates the summary of the patient", func() {
Expect(client.UpdatePatientSummary(context.Background(), patientID, patientSummary)).To(Succeed())
Expect(requestPath).To(Equal("/v1/patients/" + patientID + "/summary"))
decoded := &clinic.PatientSummaryV1{}
Expect(json.Unmarshal(requestBody, decoded)).To(Succeed())
Expect(decoded.CgmStats).ToNot(BeNil())
Expect(decoded.CgmStats.Id).To(Equal(patientSummary.CgmStats.Id))
Expect(decoded.BgmStats).To(BeNil())
})
// The clinic service reports a user who is not a patient of any clinic as no change. Most
// users are not, so reporting that as a failure would fail the work of nearly every user.
It("returns no error when the user is not a patient of any clinic", func() {
responseStatusCode = http.StatusNoContent
Expect(client.UpdatePatientSummary(context.Background(), patientID, patientSummary)).To(Succeed())
})
It("returns no error when the clinic service reports not found", func() {
responseStatusCode = http.StatusNotFound
Expect(client.UpdatePatientSummary(context.Background(), patientID, patientSummary)).To(Succeed())
})
It("returns an error when the clinic service reports an unexpected status", func() {
responseStatusCode = http.StatusInternalServerError
err := client.UpdatePatientSummary(context.Background(), patientID, patientSummary)
Expect(err).To(MatchError(ContainSubstring("unexpected response status code 500")))
})
})
Context("DeletePatientSummary", func() {
var summaryID string
BeforeEach(func() {
responseStatusCode = http.StatusOK
summaryID = primitive.NewObjectID().Hex()
})
It("deletes the summary from every patient record holding it", func() {
Expect(client.DeletePatientSummary(context.Background(), summaryID)).To(Succeed())
Expect(requestPath).To(Equal("/v1/summaries/" + summaryID + "/clinics"))
})
It("returns no error when no patient record holds the summary", func() {
responseStatusCode = http.StatusNoContent
Expect(client.DeletePatientSummary(context.Background(), summaryID)).To(Succeed())
})
// A delete resent by retried work may target a summary the clinic service already deleted
It("returns no error when the summary is not found", func() {
responseStatusCode = http.StatusNotFound
Expect(client.DeletePatientSummary(context.Background(), summaryID)).To(Succeed())
})
It("returns an error when the clinic service reports an unexpected status", func() {
responseStatusCode = http.StatusInternalServerError
err := client.DeletePatientSummary(context.Background(), summaryID)
Expect(err).To(MatchError(ContainSubstring("unexpected response status code 500")))
})
})
})