github.com/prysmaticlabs/prysm@v1.4.4/shared/tos/tos_test.go (about) 1 package tos 2 3 import ( 4 "flag" 5 "io/ioutil" 6 "os" 7 "path/filepath" 8 "testing" 9 10 "github.com/prysmaticlabs/prysm/shared/cmd" 11 "github.com/prysmaticlabs/prysm/shared/testutil/require" 12 "github.com/urfave/cli/v2" 13 ) 14 15 func TestVerifyTosAcceptedOrPrompt(t *testing.T) { 16 app := cli.App{} 17 set := flag.NewFlagSet("test", 0) 18 set.String(cmd.DataDirFlag.Name, "./tmpdir/", "") 19 context := cli.NewContext(&app, set, nil) 20 21 // replacing stdin 22 tmpfile, err := ioutil.TempFile("", "tmp") 23 require.NoError(t, err) 24 origStdin := os.Stdin 25 os.Stdin = tmpfile 26 defer func() { os.Stdin = origStdin }() 27 28 // prompt decline 29 _, err = tmpfile.Write([]byte("decline")) 30 require.NoError(t, err) 31 _, err = tmpfile.Seek(0, 0) 32 require.NoError(t, err) 33 require.ErrorContains(t, "you have to accept Terms and Conditions", VerifyTosAcceptedOrPrompt(context)) 34 35 // prompt accept 36 err = tmpfile.Truncate(0) 37 require.NoError(t, err) 38 _, err = tmpfile.Seek(0, 0) 39 require.NoError(t, err) 40 _, err = tmpfile.Write([]byte("accept")) 41 require.NoError(t, err) 42 _, err = tmpfile.Seek(0, 0) 43 require.NoError(t, err) 44 require.NoError(t, VerifyTosAcceptedOrPrompt(context)) 45 require.NoError(t, os.Remove(filepath.Join(context.String(cmd.DataDirFlag.Name), acceptTosFilename))) 46 47 require.NoError(t, tmpfile.Close()) 48 require.NoError(t, os.Remove(tmpfile.Name())) 49 50 // saved in file 51 require.NoError(t, ioutil.WriteFile(filepath.Join(context.String(cmd.DataDirFlag.Name), acceptTosFilename), []byte(""), 0666)) 52 require.NoError(t, VerifyTosAcceptedOrPrompt(context)) 53 require.NoError(t, os.RemoveAll(context.String(cmd.DataDirFlag.Name))) 54 55 // flag is set 56 set.Bool(cmd.AcceptTosFlag.Name, true, "") 57 require.NoError(t, VerifyTosAcceptedOrPrompt(context)) 58 require.NoError(t, os.RemoveAll(context.String(cmd.DataDirFlag.Name))) 59 }