github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/kbfs/kbpagesconfig/editor_test.go (about) 1 // Copyright 2018 Keybase Inc. All rights reserved. 2 // Use of this source code is governed by a BSD 3 // license that can be found in the LICENSE file. 4 5 package main 6 7 import ( 8 "context" 9 "os" 10 "path/filepath" 11 "testing" 12 13 "github.com/keybase/client/go/kbfs/libpages/config" 14 "github.com/stretchr/testify/require" 15 ) 16 17 func TestEditorACLs(t *testing.T) { 18 configDir, err := os.MkdirTemp(".", "kbpagesconfig-editor-test-") 19 require.NoError(t, err) 20 defer os.RemoveAll(configDir) 21 kbpConfigPath := filepath.Join(configDir, config.DefaultConfigFilename) 22 23 nextResponse := make(chan string, 4) 24 prompter := &fakePrompterForTest{ 25 nextResponse: nextResponse, 26 } 27 28 editor, err := newKBPConfigEditorWithPrompter( 29 configDir, prompter) 30 require.NoError(t, err) 31 // The config file shouldn't exist yet. 32 _, err = os.Stat(kbpConfigPath) 33 require.Error(t, err) 34 require.True(t, os.IsNotExist(err)) 35 // Call confirmAndWrite which should create the config file. 36 nextResponse <- "y" 37 err = editor.confirmAndWrite() 38 require.NoError(t, err) 39 _, err = os.Stat(kbpConfigPath) 40 require.NoError(t, err) 41 42 ctx := context.Background() 43 44 t.Logf("it's an empty config now so authentication should fail") 45 editor, err = newKBPConfigEditorWithPrompter( 46 configDir, prompter) 47 require.NoError(t, err) 48 ok := editor.kbpConfig.Authenticate(ctx, "alice", "12345") 49 require.False(t, ok) 50 t.Logf(`try adding a user "alice" with password "12345" and "bob" with password "54321"`) 51 nextResponse <- "n" 52 nextResponse <- "12345" 53 err = editor.setUser("alice", true) 54 require.NoError(t, err) 55 nextResponse <- "n" 56 nextResponse <- "54321" 57 err = editor.setUser("bob", true) 58 require.NoError(t, err) 59 nextResponse <- "y" 60 err = editor.confirmAndWrite() 61 require.NoError(t, err) 62 editor, err = newKBPConfigEditorWithPrompter( 63 configDir, prompter) 64 require.NoError(t, err) 65 ok = editor.kbpConfig.Authenticate(ctx, "alice", "12345") 66 require.True(t, ok) 67 ok = editor.kbpConfig.Authenticate(ctx, "bob", "54321") 68 require.True(t, ok) 69 70 t.Logf("remove bob") 71 editor, err = newKBPConfigEditorWithPrompter( 72 configDir, prompter) 73 require.NoError(t, err) 74 editor.removeUser("bob") 75 require.NoError(t, err) 76 nextResponse <- "y" 77 err = editor.confirmAndWrite() 78 require.NoError(t, err) 79 // Re-read the config file and make sure "bob" is gone and "alice" is still 80 // there. 81 editor, err = newKBPConfigEditorWithPrompter( 82 configDir, prompter) 83 require.NoError(t, err) 84 ok = editor.kbpConfig.Authenticate(ctx, "bob", "54321") 85 require.False(t, ok) 86 ok = editor.kbpConfig.Authenticate(ctx, "alice", "12345") 87 require.True(t, ok) 88 89 t.Logf("default permission for root is read") 90 editor, err = newKBPConfigEditorWithPrompter( 91 configDir, prompter) 92 require.NoError(t, err) 93 read, list, _, _, _, err := editor.kbpConfig.GetPermissions("/", nil) 94 require.NoError(t, err) 95 require.True(t, read) 96 require.False(t, list) 97 98 alice := "alice" 99 t.Logf("grant alice additional permissions") 100 editor, err = newKBPConfigEditorWithPrompter( 101 configDir, prompter) 102 require.NoError(t, err) 103 read, list, _, _, _, err = editor.kbpConfig.GetPermissions( 104 "/", &alice) 105 require.NoError(t, err) 106 require.True(t, read) 107 require.False(t, list) 108 err = editor.setAdditionalPermission("alice", "list", "/") 109 require.NoError(t, err) 110 nextResponse <- "y" 111 err = editor.confirmAndWrite() 112 require.NoError(t, err) 113 editor, err = newKBPConfigEditorWithPrompter( 114 configDir, prompter) 115 require.NoError(t, err) 116 read, list, _, _, _, err = editor.kbpConfig.GetPermissions( 117 "/", &alice) 118 require.NoError(t, err) 119 require.True(t, read) 120 require.True(t, list) 121 122 t.Logf("grant anonymous list permissions as well") 123 err = editor.setAnonymousPermission("read,list", "/") 124 require.NoError(t, err) 125 nextResponse <- "y" 126 err = editor.confirmAndWrite() 127 require.NoError(t, err) 128 editor, err = newKBPConfigEditorWithPrompter( 129 configDir, prompter) 130 require.NoError(t, err) 131 read, list, _, _, _, err = editor.kbpConfig.GetPermissions("/", nil) 132 require.NoError(t, err) 133 require.True(t, read) 134 require.True(t, list) 135 136 } 137 138 func TestEditorSimple(t *testing.T) { 139 configDir, err := os.MkdirTemp(".", "kbpagesconfig-editor-test-") 140 require.NoError(t, err) 141 defer os.RemoveAll(configDir) 142 143 nextResponse := make(chan string, 4) 144 prompter := &fakePrompterForTest{ 145 nextResponse: nextResponse, 146 } 147 148 editor, err := newKBPConfigEditorWithPrompter( 149 configDir, prompter) 150 require.NoError(t, err) 151 err = editor.setAccessControlAllowOrigin("/", "") 152 require.NoError(t, err) 153 nextResponse <- "y" 154 err = editor.confirmAndWrite() 155 require.NoError(t, err) 156 157 editor, err = newKBPConfigEditorWithPrompter( 158 configDir, prompter) 159 require.NoError(t, err) 160 err = editor.setAccessControlAllowOrigin("/", "*") 161 require.NoError(t, err) 162 nextResponse <- "y" 163 err = editor.confirmAndWrite() 164 require.NoError(t, err) 165 166 editor, err = newKBPConfigEditorWithPrompter( 167 configDir, prompter) 168 require.NoError(t, err) 169 err = editor.setAccessControlAllowOrigin("/", "https://keybase.io") 170 require.Error(t, err) 171 172 editor, err = newKBPConfigEditorWithPrompter( 173 configDir, prompter) 174 require.NoError(t, err) 175 err = editor.set403("/", "/403.html") 176 require.NoError(t, err) 177 nextResponse <- "y" 178 err = editor.confirmAndWrite() 179 require.NoError(t, err) 180 181 editor, err = newKBPConfigEditorWithPrompter( 182 configDir, prompter) 183 require.NoError(t, err) 184 err = editor.set404("/", "/404") 185 require.NoError(t, err) 186 187 editor, err = newKBPConfigEditorWithPrompter( 188 configDir, prompter) 189 require.NoError(t, err) 190 err = editor.set404("/", "../404.html") 191 require.Error(t, err) 192 }