96 lines
2.7 KiB
Go
96 lines
2.7 KiB
Go
/*
|
|
* Ekster is a microsub server
|
|
* Copyright (c) 2021 The Ekster authors
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
package main
|
|
|
|
import (
|
|
"io/ioutil"
|
|
"math/rand"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"net/url"
|
|
"testing"
|
|
|
|
"github.com/gomodule/redigo/redis"
|
|
"github.com/rafaeljusto/redigomock/v3"
|
|
"github.com/stretchr/testify/assert"
|
|
"p83.nl/go/ekster/pkg/server"
|
|
"p83.nl/go/ekster/pkg/util"
|
|
)
|
|
|
|
func init() {
|
|
rand.Seed(1)
|
|
}
|
|
|
|
func TestMainHandler_ServeHTTP_NoArgs(t *testing.T) {
|
|
conn := redigomock.NewConn()
|
|
pool := &redis.Pool{
|
|
// Return the same connection mock for each Get() call.
|
|
Dial: func() (redis.Conn, error) { return conn, nil },
|
|
MaxIdle: 10,
|
|
}
|
|
|
|
h := mainHandler{Backend: &memoryBackend{AuthEnabled: true}, BaseURL: "", TemplateDir: "", pool: pool}
|
|
r := httptest.NewRequest(http.MethodGet, "/auth", nil)
|
|
w := httptest.NewRecorder()
|
|
h.ServeHTTP(w, r)
|
|
assert.Equal(t, 400, w.Code)
|
|
}
|
|
func TestMainHandler_ServeHTTP_Args(t *testing.T) {
|
|
conn := redigomock.NewConn()
|
|
pool := &redis.Pool{
|
|
// Return the same connection mock for each Get() call.
|
|
Dial: func() (redis.Conn, error) { return conn, nil },
|
|
MaxIdle: 10,
|
|
}
|
|
|
|
q := url.Values{}
|
|
q.Add("response_type", "code")
|
|
q.Add("client_id", "https://example.com/")
|
|
q.Add("redirect_uri", "https://example.com/callback")
|
|
q.Add("me", "https://p83.nl/")
|
|
state := util.RandStringBytes(32)
|
|
q.Add("state", state)
|
|
q.Add("scope", "create")
|
|
h := mainHandler{Backend: &server.NullBackend{}, BaseURL: "", TemplateDir: "", pool: pool}
|
|
r := httptest.NewRequest(http.MethodGet, "/auth?"+q.Encode(), nil)
|
|
w := httptest.NewRecorder()
|
|
|
|
conn.Command("HGETALL", "session:FpLSjFbcXoEFfRsW").ExpectMap(map[string]string{
|
|
"logged_in": "1",
|
|
})
|
|
conn.Command(
|
|
"HMSET", "state:XVlBzgbaiCMRAjWwhTHctcuAxhxKQFDa",
|
|
"me", "https://p83.nl/",
|
|
"client_id", "https://example.com/",
|
|
"scope", "create",
|
|
"redirect_uri", "https://example.com/callback",
|
|
"state", "XVlBzgbaiCMRAjWwhTHctcuAxhxKQFDa",
|
|
"code", "",
|
|
"channel", "",
|
|
"access_token", "",
|
|
)
|
|
|
|
h.ServeHTTP(w, r)
|
|
assert.Equal(t, 302, w.Code)
|
|
body, err := ioutil.ReadAll(w.Result().Body)
|
|
if assert.NoError(t, err) {
|
|
assert.Equal(t, "", string(body))
|
|
}
|
|
}
|