-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathheart_auth_test.go
82 lines (67 loc) · 2.26 KB
/
heart_auth_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
package auth
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/gin-gonic/gin"
"github.com/pebbe/util"
. "gopkg.in/check.v1"
)
type HEARTScopesSuite struct {
}
func Test(t *testing.T) { TestingT(t) }
var _ = Suite(&HEARTScopesSuite{})
func (s *HEARTScopesSuite) SetUpTest(c *C) {
}
func (s *HEARTScopesSuite) TestGetPatientWithoutScopes(c *C) {
rr := s.SetUpRequest("GET", "")
c.Assert(rr.Code, Equals, http.StatusForbidden)
}
func (s *HEARTScopesSuite) TestGetPatientWithWriteScopes(c *C) {
rr := s.SetUpRequest("GET", "user/Patient.write")
c.Assert(rr.Code, Equals, http.StatusForbidden)
}
func (s *HEARTScopesSuite) TestGetPatientWithScopes(c *C) {
rr := s.SetUpRequest("GET", "user/Patient.read")
c.Assert(rr.Code, Equals, http.StatusOK)
c.Assert(rr.Body.String(), Equals, "Hello")
}
func (s *HEARTScopesSuite) TestGetPatientWithMultipleScopes(c *C) {
rr := s.SetUpRequest("GET", "user/Patient.read user/Observation.* user/Condition.write")
c.Assert(rr.Code, Equals, http.StatusOK)
c.Assert(rr.Body.String(), Equals, "Hello")
}
func (s *HEARTScopesSuite) TestGetPatientWithWildcard(c *C) {
rr := s.SetUpRequest("GET", "user/Patient.*")
c.Assert(rr.Code, Equals, http.StatusOK)
c.Assert(rr.Body.String(), Equals, "Hello")
}
func (s *HEARTScopesSuite) TestGetPatientWithAllWildcard(c *C) {
rr := s.SetUpRequest("GET", "user/*.*")
c.Assert(rr.Code, Equals, http.StatusOK)
c.Assert(rr.Body.String(), Equals, "Hello")
}
func (s *HEARTScopesSuite) TestPostPatientWithScopes(c *C) {
rr := s.SetUpRequest("POST", "user/Patient.write")
c.Assert(rr.Code, Equals, http.StatusOK)
c.Assert(rr.Body.String(), Equals, "Hello")
}
func (s *HEARTScopesSuite) SetUpRequest(method, scopes string) *httptest.ResponseRecorder {
r, err := http.NewRequest(method, "/", nil)
util.CheckErr(err)
r.Header.Add("Content-Type", "application/json")
mockTokenIntrospection := func(c *gin.Context) {
if scopes != "" {
c.Set("scopes", strings.Split(scopes, " "))
}
}
e := gin.New()
rw := httptest.NewRecorder()
noop := func(c *gin.Context) { c.String(http.StatusOK, "Hello") }
authHandler := HEARTScopesHandler("Patient")
e.GET("/", mockTokenIntrospection, authHandler, noop)
e.POST("/", mockTokenIntrospection, authHandler, noop)
e.ServeHTTP(rw, r)
return rw
}