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-05-15 10:22:24 +02:00
|
|
|
// Function to test 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)
|
|
|
|
|
2017-05-03 07:16:45 +02:00
|
|
|
// HasPermission responds true
|
2017-06-09 09:42:39 +02:00
|
|
|
reached := false
|
2017-04-05 20:23:29 +02:00
|
|
|
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)
|
|
|
|
}
|