2020-10-15 02:05:30 +00:00
|
|
|
package tos
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"os"
|
2020-10-15 03:58:57 +00:00
|
|
|
"path/filepath"
|
2020-10-15 02:05:30 +00:00
|
|
|
"testing"
|
|
|
|
|
2021-09-21 18:11:16 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/cmd"
|
2021-09-23 18:53:46 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/testing/require"
|
2020-10-15 02:05:30 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestVerifyTosAcceptedOrPrompt(t *testing.T) {
|
|
|
|
app := cli.App{}
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
2020-10-15 21:30:38 +00:00
|
|
|
set.String(cmd.DataDirFlag.Name, "./tmpdir/", "")
|
2020-10-15 02:05:30 +00:00
|
|
|
context := cli.NewContext(&app, set, nil)
|
|
|
|
|
|
|
|
// replacing stdin
|
2022-04-18 20:42:07 +00:00
|
|
|
tmpfile, err := os.CreateTemp("", "tmp")
|
2020-10-15 02:05:30 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
origStdin := os.Stdin
|
|
|
|
os.Stdin = tmpfile
|
|
|
|
defer func() { os.Stdin = origStdin }()
|
|
|
|
|
2021-09-17 21:55:24 +00:00
|
|
|
// userprompt decline
|
2020-10-15 02:05:30 +00:00
|
|
|
_, err = tmpfile.Write([]byte("decline"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
_, err = tmpfile.Seek(0, 0)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.ErrorContains(t, "you have to accept Terms and Conditions", VerifyTosAcceptedOrPrompt(context))
|
|
|
|
|
2021-09-17 21:55:24 +00:00
|
|
|
// userprompt accept
|
2020-10-15 02:05:30 +00:00
|
|
|
err = tmpfile.Truncate(0)
|
|
|
|
require.NoError(t, err)
|
|
|
|
_, err = tmpfile.Seek(0, 0)
|
|
|
|
require.NoError(t, err)
|
|
|
|
_, err = tmpfile.Write([]byte("accept"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
_, err = tmpfile.Seek(0, 0)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, VerifyTosAcceptedOrPrompt(context))
|
2020-10-15 03:58:57 +00:00
|
|
|
require.NoError(t, os.Remove(filepath.Join(context.String(cmd.DataDirFlag.Name), acceptTosFilename)))
|
2020-10-15 02:05:30 +00:00
|
|
|
|
|
|
|
require.NoError(t, tmpfile.Close())
|
|
|
|
require.NoError(t, os.Remove(tmpfile.Name()))
|
|
|
|
|
|
|
|
// saved in file
|
2022-04-18 20:42:07 +00:00
|
|
|
require.NoError(t, os.WriteFile(filepath.Join(context.String(cmd.DataDirFlag.Name), acceptTosFilename), []byte(""), 0666))
|
2020-10-15 02:05:30 +00:00
|
|
|
require.NoError(t, VerifyTosAcceptedOrPrompt(context))
|
2020-10-15 21:30:38 +00:00
|
|
|
require.NoError(t, os.RemoveAll(context.String(cmd.DataDirFlag.Name)))
|
2020-10-15 02:05:30 +00:00
|
|
|
|
|
|
|
// flag is set
|
|
|
|
set.Bool(cmd.AcceptTosFlag.Name, true, "")
|
|
|
|
require.NoError(t, VerifyTosAcceptedOrPrompt(context))
|
2020-10-15 21:30:38 +00:00
|
|
|
require.NoError(t, os.RemoveAll(context.String(cmd.DataDirFlag.Name)))
|
2020-10-15 02:05:30 +00:00
|
|
|
}
|