2021-10-08 12:39:31 +00:00
|
|
|
// SPDX-FileCopyrightText: 2021 The NGI Pointer Secure-Scuttlebutt Team of 2020/2021
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
2021-03-12 13:42:30 +00:00
|
|
|
package admin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2022-11-07 09:18:13 +00:00
|
|
|
refs "github.com/ssbc/go-ssb-refs"
|
|
|
|
"github.com/ssbc/go-ssb-room/v2/roomdb"
|
|
|
|
"github.com/ssbc/go-ssb-room/v2/web/router"
|
|
|
|
"github.com/ssbc/go-ssb-room/v2/web/webassert"
|
2021-03-12 13:42:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestAliasesRevokeConfirmation(t *testing.T) {
|
|
|
|
ts := newSession(t)
|
|
|
|
a := assert.New(t)
|
|
|
|
|
|
|
|
testKey, err := refs.ParseFeedRef("@x7iOLUcq3o+sjGeAnipvWeGzfuYgrXl8L4LYlxIhwDc=.ed25519")
|
|
|
|
a.NoError(err)
|
2022-11-07 09:18:13 +00:00
|
|
|
testEntry := roomdb.Alias{ID: 666, Name: "the-test-name", Feed: testKey}
|
2021-03-19 09:43:46 +00:00
|
|
|
ts.AliasesDB.GetByIDReturns(testEntry, nil)
|
2021-03-12 13:42:30 +00:00
|
|
|
|
2021-04-01 07:04:38 +00:00
|
|
|
urlRevokeConfirm := ts.URLTo(router.AdminAliasesRevokeConfirm, "id", 3)
|
2021-03-12 13:42:30 +00:00
|
|
|
|
2021-04-01 07:04:38 +00:00
|
|
|
html, resp := ts.Client.GetHTML(urlRevokeConfirm)
|
2021-03-12 13:42:30 +00:00
|
|
|
a.Equal(http.StatusOK, resp.Code, "wrong HTTP status code")
|
|
|
|
|
2022-11-07 09:18:13 +00:00
|
|
|
a.Equal(testKey.String(), html.Find("pre#verify").Text(), "has the key for verification")
|
2021-03-12 13:42:30 +00:00
|
|
|
|
|
|
|
form := html.Find("form#confirm")
|
|
|
|
|
|
|
|
method, ok := form.Attr("method")
|
|
|
|
a.True(ok, "form has method set")
|
|
|
|
a.Equal("POST", method)
|
|
|
|
|
|
|
|
action, ok := form.Attr("action")
|
|
|
|
a.True(ok, "form has action set")
|
|
|
|
|
2021-04-01 07:04:38 +00:00
|
|
|
addURL := ts.URLTo(router.AdminAliasesRevoke)
|
2021-04-19 06:28:45 +00:00
|
|
|
a.Equal(addURL.String(), action)
|
2021-03-12 13:42:30 +00:00
|
|
|
|
2021-03-16 13:16:48 +00:00
|
|
|
webassert.ElementsInForm(t, form, []webassert.FormElement{
|
2021-03-12 13:42:30 +00:00
|
|
|
{Name: "name", Type: "hidden", Value: testEntry.Name},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAliasesRevoke(t *testing.T) {
|
|
|
|
ts := newSession(t)
|
|
|
|
a := assert.New(t)
|
|
|
|
|
2021-04-01 07:04:38 +00:00
|
|
|
urlRevoke := ts.URLTo(router.AdminAliasesRevoke)
|
2021-04-12 09:23:11 +00:00
|
|
|
overviewURL := ts.URLTo(router.AdminMembersOverview)
|
2021-03-12 13:42:30 +00:00
|
|
|
|
2021-04-26 06:28:42 +00:00
|
|
|
aliasEntry := roomdb.Alias{
|
|
|
|
ID: ts.User.ID,
|
|
|
|
Feed: ts.User.PubKey,
|
|
|
|
Name: "Blobby",
|
|
|
|
}
|
2021-03-19 09:43:46 +00:00
|
|
|
ts.AliasesDB.RevokeReturns(nil)
|
2021-04-26 06:28:42 +00:00
|
|
|
ts.AliasesDB.ResolveReturns(aliasEntry, nil)
|
2021-03-12 13:42:30 +00:00
|
|
|
|
|
|
|
addVals := url.Values{"name": []string{"the-name"}}
|
2021-04-01 07:04:38 +00:00
|
|
|
rec := ts.Client.PostForm(urlRevoke, addVals)
|
2021-05-13 07:57:45 +00:00
|
|
|
a.Equal(http.StatusSeeOther, rec.Code)
|
2021-04-01 07:04:38 +00:00
|
|
|
a.Equal(overviewURL.Path, rec.Header().Get("Location"))
|
|
|
|
a.True(len(rec.Result().Cookies()) > 0, "got a cookie")
|
|
|
|
|
2021-04-12 09:23:11 +00:00
|
|
|
webassert.HasFlashMessages(t, ts.Client, overviewURL, "AdminMemberDetailsAliasRevoked")
|
2021-03-12 13:42:30 +00:00
|
|
|
|
2021-03-19 09:43:46 +00:00
|
|
|
a.Equal(1, ts.AliasesDB.RevokeCallCount())
|
|
|
|
_, theName := ts.AliasesDB.RevokeArgsForCall(0)
|
2021-03-12 13:42:30 +00:00
|
|
|
a.EqualValues("the-name", theName)
|
|
|
|
|
|
|
|
// now for unknown ID
|
2021-03-19 09:43:46 +00:00
|
|
|
ts.AliasesDB.RevokeReturns(roomdb.ErrNotFound)
|
2021-03-12 13:42:30 +00:00
|
|
|
addVals = url.Values{"name": []string{"nope"}}
|
2021-04-01 07:04:38 +00:00
|
|
|
rec = ts.Client.PostForm(urlRevoke, addVals)
|
2021-05-13 07:57:45 +00:00
|
|
|
a.Equal(http.StatusSeeOther, rec.Code)
|
2021-04-01 07:04:38 +00:00
|
|
|
a.Equal(overviewURL.Path, rec.Header().Get("Location"))
|
|
|
|
a.True(len(rec.Result().Cookies()) > 0, "got a cookie")
|
|
|
|
|
2021-04-12 09:23:11 +00:00
|
|
|
webassert.HasFlashMessages(t, ts.Client, overviewURL, "ErrorNotFound")
|
2021-03-12 13:42:30 +00:00
|
|
|
}
|