aboutsummaryrefslogtreecommitdiff
path: root/internal/webui/root_test.go
diff options
context:
space:
mode:
authorJulien Dessaux2021-04-21 17:23:07 +0200
committerJulien Dessaux2021-04-21 17:23:07 +0200
commit4a2fb7e82d5d617298cb28b66485fc6f30c55781 (patch)
tree4ae55d8208e3ebf603f3a17ac56f6efd0e011efc /internal/webui/root_test.go
parentImplemented the ResumeSession function (diff)
downloadtrains-4a2fb7e82d5d617298cb28b66485fc6f30c55781.tar.gz
trains-4a2fb7e82d5d617298cb28b66485fc6f30c55781.tar.bz2
trains-4a2fb7e82d5d617298cb28b66485fc6f30c55781.zip
Reworked the webui package, added authentication feature and tests
Diffstat (limited to '')
-rw-r--r--internal/webui/root_test.go54
1 files changed, 54 insertions, 0 deletions
diff --git a/internal/webui/root_test.go b/internal/webui/root_test.go
new file mode 100644
index 0000000..8faee77
--- /dev/null
+++ b/internal/webui/root_test.go
@@ -0,0 +1,54 @@
+package webui
+
+import (
+ "net/http"
+ "testing"
+
+ "git.adyxax.org/adyxax/trains/pkg/database"
+ "git.adyxax.org/adyxax/trains/pkg/model"
+ "github.com/stretchr/testify/require"
+)
+
+func TestRootHandler(t *testing.T) {
+ // test environment setup
+ dbEnv, err := database.InitDB("sqlite3", "file::memory:?_foreign_keys=on")
+ require.Nil(t, err)
+ err = dbEnv.Migrate()
+ require.Nil(t, err)
+ user1, err := dbEnv.CreateUser(&model.UserRegistration{Username: "user1", Password: "password1", Email: "julien@adyxax.org"})
+ require.Nil(t, err)
+ _, err = dbEnv.Login(&model.UserLogin{Username: "user1", Password: "password1"})
+ require.Nil(t, err)
+ token1, err := dbEnv.CreateSession(user1)
+ require.Nil(t, err)
+ e := &env{
+ dbEnv: dbEnv,
+ // TODO mock navitia
+ }
+ // test GET requests
+ runHttpTest(t, e, rootHandler, &httpTestCase{
+ name: "a simple get when not logged in should redirect to the login page",
+ input: httpTestInput{
+ method: http.MethodGet,
+ path: "/",
+ },
+ expect: httpTestExpect{
+ code: http.StatusFound,
+ location: "/login",
+ },
+ })
+ // TODO mock navitia
+ _ = token1
+ //runHttpTest(t, e, rootHandler, &httpTestCase{
+ // name: "a simple get when logged in should display the departure times",
+ // input: httpTestInput{
+ // method: http.MethodGet,
+ // path: "/",
+ // cookie: &http.Cookie{Name: sessionCookieName, Value: *token1},
+ // },
+ // expect: httpTestExpect{
+ // code: http.StatusOK,
+ // bodyString: "Horaires des prochains trains",
+ // },
+ //})
+}