forked from whytheplatypus/go-bluebutton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bluebutton_test.go
179 lines (156 loc) · 4.22 KB
/
bluebutton_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
package bluebutton_test
import (
"context"
"encoding/json"
"flag"
"io/ioutil"
"net/http"
"net/http/httputil"
"os"
"strings"
"testing"
"golang.org/x/oauth2"
bluebutton "github.com/whytheplatypus/go-bluebutton"
)
var (
RedirectURL string
bbURL string
CLIENT_ID string
CLIENT_SECRET string
)
func init() {
flag.StringVar(&bbURL,
"url",
"http://localhost:8000",
"The URL of the bluebutton API")
flag.StringVar(&CLIENT_SECRET,
"secret",
"",
"The oauth2 client secret for your bluebutton application")
flag.StringVar(&CLIENT_ID,
"id",
"",
"The oauth2 client id for your bluebutton application")
flag.StringVar(&RedirectURL,
"redirect-url",
"http://127.0.0.1:8080/testclient/callback",
"The url for your oauth2 callback endpoint")
}
func TestMain(m *testing.M) {
flag.Parse()
os.Exit(m.Run())
}
func TestExchangeWithBadCode(t *testing.T) {
if CLIENT_ID == "" || CLIENT_SECRET == "" {
t.Skip()
}
code := "bogus"
conf := &oauth2.Config{
RedirectURL: RedirectURL,
ClientID: CLIENT_ID,
ClientSecret: CLIENT_SECRET,
Endpoint: oauth2.Endpoint{
AuthURL: bbURL + "/v1/o/authorize/",
TokenURL: bbURL + "/v1/o/token/",
},
}
_, err := conf.Exchange(context.Background(), code)
result := `oauth2: cannot fetch token: 401 Unauthorized
Response: {"error": "invalid_grant"}`
if strings.Compare(err.Error(), result) != 0 {
t.Error(err)
}
}
func TestPasswordCredentialsToken(t *testing.T) {
if CLIENT_ID == "" || CLIENT_SECRET == "" {
t.Skip()
}
conf := &oauth2.Config{
RedirectURL: RedirectURL,
ClientID: CLIENT_ID,
ClientSecret: CLIENT_SECRET,
Endpoint: oauth2.Endpoint{
AuthURL: bbURL + "/v1/o/authorize/",
TokenURL: bbURL + "/v1/o/token/",
},
}
_, err := conf.PasswordCredentialsToken(context.Background(), "blah", "blahblah")
result := `oauth2: cannot fetch token: 401 Unauthorized
Response: {"error_description": "Invalid credentials given.", "error": "invalid_grant"}`
altResult := `oauth2: cannot fetch token: 401 Unauthorized
Response: {"error": "invalid_grant", "error_description": "Invalid credentials given."}`
if strings.Compare(err.Error(), result) != 0 && strings.Compare(err.Error(), altResult) != 0 {
t.Error(err)
}
}
func TestRefreshToken(t *testing.T) {
if CLIENT_ID == "" || CLIENT_SECRET == "" {
t.Skip()
}
conf := &oauth2.Config{
RedirectURL: RedirectURL,
ClientID: CLIENT_ID,
ClientSecret: CLIENT_SECRET,
Endpoint: oauth2.Endpoint{
AuthURL: bbURL + "/v1/o/authorize/",
TokenURL: bbURL + "/v1/o/token/",
},
}
tkn := `{
"access_token": "old-bogus",
"token_type": "Bearer",
"refresh_token": "bogus",
"expiry": "2018-01-01T00:00:00-05:00"
}`
tok := &oauth2.Token{}
if err := json.Unmarshal([]byte(tkn), tok); err != nil {
t.Fatal(err)
}
tknClient := conf.Client(context.Background(), tok)
_, err := tknClient.Get(bbURL + "/v1/fhir/ExplanationOfBenefit/?patient=20140000008325")
//r, _ := httputil.DumpResponse(rsp, true)
//log.Println(string(r))
result := `Get https://sandbox.bluebutton.cms.gov/v1/fhir/ExplanationOfBenefit/?patient=20140000008325: oauth2: cannot fetch token: 401 Unauthorized
Response: {"error": "invalid_grant"}`
if strings.Compare(err.Error(), result) != 0 {
t.Error(err)
}
}
func BenchmarkEOB(b *testing.B) {
for i := 0; i < b.N; i++ {
bluebutton.BB_URL = bbURL
conf := &oauth2.Config{
RedirectURL: RedirectURL,
ClientID: CLIENT_ID,
ClientSecret: CLIENT_SECRET,
Endpoint: oauth2.Endpoint{
AuthURL: bluebutton.AuthURL(),
TokenURL: bluebutton.TokenURL(),
},
}
tkn, err := ioutil.ReadFile(".tkn")
if err != nil {
b.Fatal(err)
}
tok := &oauth2.Token{}
if err := json.Unmarshal(tkn, tok); err != nil {
b.Fatal(err)
}
tknClient := conf.Client(context.Background(), tok)
rsp, err := tknClient.Get(bbURL + "/v1/fhir/ExplanationOfBenefit/")
//rsp, err := tknClient.Get(bbURL + "/v1/fhir/Patient/20140000008325")
//rsp, err := tknClient.Get(bbURL + "/v1/fhir/Nothing")
if err != nil {
b.Fatal(err)
}
if rsp.StatusCode != http.StatusOK {
b.Fatal(rsp)
}
httputil.DumpResponse(rsp, true)
eob, _ := ioutil.ReadAll(rsp.Body)
b.Log(len(eob))
//var out bytes.Buffer
//json.Indent(&out, eob, " ", "\t")
//fmt.Println(out.String())
}
}