-
-
Notifications
You must be signed in to change notification settings - Fork 178
/
main_test.go
65 lines (58 loc) · 2.19 KB
/
main_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
package main_test
import (
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/httptest"
"github.com/kataras/iris/v12/middleware/basicauth"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("Ginkgotest", func() {
var (
e *httptest.Expect
app *iris.Application
opts basicauth.Options
authentication iris.Handler // or just: basicauth.Default(map...)
)
BeforeEach(func() {
opts = basicauth.Options{
Allow: basicauth.AllowUsers(map[string]string{"myusername": "mypassword"}),
}
authentication = basicauth.New(opts)
app = newApp(authentication)
e = httptest.New(GinkgoT(), app, httptest.Strict(true))
})
When("no basic auth", Ordered, func() {
It("redirects to /admin without basic auth", func() {
response := e.GET("/").Expect().Raw()
Expect(httptest.StatusUnauthorized).To(Equal(response.StatusCode))
})
It("without basic auth", func() {
// without basic auth
response := e.GET("/").Expect().Raw()
Expect(httptest.StatusUnauthorized).To(Equal(response.StatusCode))
})
})
When("valid basic auth", func() {
It("with basic auth /admin", func() {
expect := e.GET("/admin").WithBasicAuth("myusername", "mypassword").Expect()
Expect(httptest.StatusOK).To(Equal(expect.Raw().StatusCode))
Expect("/admin myusername:mypassword").To(Equal(expect.Body().Raw()))
})
It("with basic auth /admin/profile", func() {
expect := e.GET("/admin/profile").WithBasicAuth("myusername", "mypassword").Expect()
Expect(httptest.StatusOK).To(Equal(expect.Raw().StatusCode))
Expect("/admin/profile myusername:mypassword").To(Equal(expect.Body().Raw()))
})
It("with basic auth /admin/profile", func() {
expect := e.GET("/admin/settings").WithBasicAuth("myusername", "mypassword").Expect()
Expect(httptest.StatusOK).To(Equal(expect.Raw().StatusCode))
Expect("/admin/settings myusername:mypassword").To(Equal(expect.Body().Raw()))
})
})
When("invalid basic auth", func() {
It("invalid basic auth /admin/settings", func() {
expect := e.GET("/admin/settings").WithBasicAuth("invalidusername", "invalidpassword").Expect()
Expect(httptest.StatusUnauthorized).To(Equal(expect.Raw().StatusCode))
})
})
})