60 lines
1.4 KiB
Go
60 lines
1.4 KiB
Go
package users
|
|
|
|
import (
|
|
"io/ioutil"
|
|
"net/http"
|
|
"net/http/cookiejar"
|
|
"net/http/httptest"
|
|
"net/url"
|
|
"testing"
|
|
|
|
"github.com/Sirupsen/logrus"
|
|
"github.com/jmoiron/sqlx"
|
|
|
|
"gitlab.quimbo.fr/odwrtw/canape-sql/auth"
|
|
"gitlab.quimbo.fr/odwrtw/canape-sql/sqly"
|
|
"gitlab.quimbo.fr/odwrtw/canape-sql/web"
|
|
)
|
|
|
|
type UserBackend struct {
|
|
db *sqlx.DB
|
|
}
|
|
|
|
func (b *UserBackend) Get(username string) (auth.User, error) {
|
|
return Get(db, username)
|
|
}
|
|
|
|
func createUserBackend(db *sqlx.DB) *UserBackend {
|
|
return &UserBackend{db: db}
|
|
}
|
|
|
|
func TestLoginSubmit(t *testing.T) {
|
|
sqly.RunWithLastestMigration(db, pgdsn, t, func(db *sqlx.DB, t *testing.T) {
|
|
backend := createUserBackend(db)
|
|
authorizer := auth.New(backend, "peeper", "cookieName", "cookieKey", 10)
|
|
e := web.NewEnv(db, authorizer, logrus.NewEntry(logrus.New()), "../templates")
|
|
e.Mode = web.TestMode
|
|
e.Router.Handle("/login", e.Handler(loginSubmitHandler)).Methods("POST").Name("loginPost")
|
|
|
|
ts := httptest.NewServer(e.Router)
|
|
defer ts.Close()
|
|
|
|
cookieJar, _ := cookiejar.New(nil)
|
|
client := &http.Client{
|
|
Jar: cookieJar,
|
|
}
|
|
|
|
v := url.Values{}
|
|
v.Add("username", "plop")
|
|
v.Add("password", "ploppwd")
|
|
resp, err := client.PostForm(ts.URL+"/login", v)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
str, _ := ioutil.ReadAll(resp.Body)
|
|
if string(str) != `{"RouteName":"loginPost","Data":{"FormErrors":{"password":"Invalid username or password"}}}` {
|
|
t.Fatalf("Unpected content: %s", string(str))
|
|
}
|
|
})
|
|
}
|