genofire/hs_monolith
genofire
/
hs_monolith
Archived
1
0
Fork 0
This repository has been archived on 2020-09-27. You can view files and clone it, but cannot push or open issues or pull requests.
hs_monolith/lib/http/permission_test.go

58 lines
1.3 KiB
Go

// Package that provides the logic of the webserver
package http
import (
"errors"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
// Function to the the permission and it's error handling
func TestPermission(t *testing.T) {
assert := assert.New(t)
w := httptest.NewRecorder()
r, _ := http.NewRequest("GET", "/", nil)
// Request without session cookie
reached := false
PermissionHandler(func(w http.ResponseWriter, r *http.Request) {
reached = true
}, func(s string, i int) (bool, error) {
return true, nil
}, 1)(w, r)
assert.False(reached)
r.AddCookie(&http.Cookie{Name: "session"})
// HasPermission responds true
reached = false
PermissionHandler(func(w http.ResponseWriter, r *http.Request) {
reached = true
}, func(s string, i int) (bool, error) {
return true, nil
}, 1)(w, r)
assert.True(reached)
// HasPermission responds false
reached = false
PermissionHandler(func(w http.ResponseWriter, r *http.Request) {
reached = true
}, func(s string, i int) (bool, error) {
return false, nil
}, 1)(w, r)
assert.False(reached)
// HasPermission responds error
reached = false
PermissionHandler(func(w http.ResponseWriter, r *http.Request) {
reached = true
}, func(s string, i int) (bool, error) {
return false, errors.New("text")
}, 1)(w, r)
assert.False(reached)
}