2023-01-03 09:18:30 +00:00
|
|
|
// Copyright (C) 2019-2023 Nicola Murino
|
2022-07-17 18:16:00 +00:00
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published
|
|
|
|
// by the Free Software Foundation, version 3.
|
|
|
|
//
|
|
|
|
// 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 Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
2023-01-03 09:18:30 +00:00
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2022-07-17 18:16:00 +00:00
|
|
|
|
2022-02-13 13:30:20 +00:00
|
|
|
package httpd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
2023-12-12 18:04:32 +00:00
|
|
|
"encoding/json"
|
2022-02-13 13:30:20 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2023-12-12 18:04:32 +00:00
|
|
|
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/util"
|
2022-02-13 13:30:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestFlashMessages(t *testing.T) {
|
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
req, err := http.NewRequest(http.MethodGet, "/url", nil)
|
|
|
|
require.NoError(t, err)
|
2023-12-12 18:04:32 +00:00
|
|
|
message := flashMessage{
|
|
|
|
ErrorString: "error",
|
|
|
|
I18nMessage: util.I18nChangePwdTitle,
|
|
|
|
}
|
|
|
|
setFlashMessage(rr, req, message)
|
|
|
|
value, err := json.Marshal(message)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
req.Header.Set("Cookie", fmt.Sprintf("%v=%v", flashCookieName, base64.URLEncoding.EncodeToString(value)))
|
2022-02-13 13:30:20 +00:00
|
|
|
msg := getFlashMessage(rr, req)
|
2023-12-12 18:04:32 +00:00
|
|
|
assert.Equal(t, message, msg)
|
|
|
|
assert.Equal(t, util.I18nChangePwdTitle, msg.getI18nError().Message)
|
2022-02-13 13:30:20 +00:00
|
|
|
req.Header.Set("Cookie", fmt.Sprintf("%v=%v", flashCookieName, "a"))
|
|
|
|
msg = getFlashMessage(rr, req)
|
|
|
|
assert.Empty(t, msg)
|
2023-12-12 18:04:32 +00:00
|
|
|
req.Header.Set("Cookie", fmt.Sprintf("%v=%v", flashCookieName, "YQ=="))
|
|
|
|
msg = getFlashMessage(rr, req)
|
|
|
|
assert.Empty(t, msg)
|
2022-02-13 13:30:20 +00:00
|
|
|
}
|