2017-05-03 07:16:45 +02:00
|
|
|
// Package that provides the logic of the webserver
|
2017-04-05 20:23:29 +02:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2017-04-28 10:10:25 +02:00
|
|
|
// Function to the the permission and it's error handling
|
2017-04-05 20:23:29 +02:00
|
|
|
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"})
|
|
|
|
|
2017-05-03 07:16:45 +02:00
|
|
|
// HasPermission responds true
|
2017-04-05 20:23:29 +02:00
|
|
|
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)
|
|
|
|
|
2017-05-03 07:16:45 +02:00
|
|
|
// HasPermission responds false
|
2017-04-05 20:23:29 +02:00
|
|
|
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)
|
|
|
|
|
2017-05-03 07:16:45 +02:00
|
|
|
// HasPermission responds error
|
2017-04-05 20:23:29 +02:00
|
|
|
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)
|
|
|
|
}
|