|
| 1 | +// |
| 2 | +// DISCLAIMER |
| 3 | +// |
| 4 | +// Copyright 2017 ArangoDB GmbH, Cologne, Germany |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | +// |
| 18 | +// Copyright holder is ArangoDB GmbH, Cologne, Germany |
| 19 | +// |
| 20 | +// Author Ewout Prangsma |
| 21 | +// |
| 22 | + |
| 23 | +// +build auth |
| 24 | + |
| 25 | +package test |
| 26 | + |
| 27 | +import ( |
| 28 | + "context" |
| 29 | + "testing" |
| 30 | + |
| 31 | + driver "github.com/arangodb/go-driver" |
| 32 | +) |
| 33 | + |
| 34 | +// TestServerModeAndGrants checks user access grants in combination with |
| 35 | +// server mode and WithConfigured. |
| 36 | +func TestServerModeAndGrants(t *testing.T) { |
| 37 | + c := createClientFromEnv(t, true) |
| 38 | + ctx := context.Background() |
| 39 | + |
| 40 | + version, err := c.Version(nil) |
| 41 | + if err != nil { |
| 42 | + t.Fatalf("Version failed: %s", describe(err)) |
| 43 | + } |
| 44 | + isv33p := version.Version.CompareTo("3.3") >= 0 |
| 45 | + if !isv33p { |
| 46 | + t.Skip("This test requires version 3.3") |
| 47 | + } else { |
| 48 | + // Get root user |
| 49 | + u, err := c.User(ctx, "root") |
| 50 | + if err != nil { |
| 51 | + t.Fatalf("User('root') failed: %s", describe(err)) |
| 52 | + } |
| 53 | + |
| 54 | + // Initial server mode must be default |
| 55 | + if mode, err := c.ServerMode(ctx); err != nil { |
| 56 | + t.Fatalf("ServerMode failed: %s", describe(err)) |
| 57 | + } else if mode != driver.ServerModeDefault { |
| 58 | + t.Errorf("ServerMode returned '%s', but expected '%s'", mode, driver.ServerModeDefault) |
| 59 | + } |
| 60 | + |
| 61 | + // Create simple collection |
| 62 | + db := ensureDatabase(ctx, c, "_system", nil, t) |
| 63 | + colName := "server_mode_and_grants_test1" |
| 64 | + col := ensureCollection(ctx, db, colName, nil, t) |
| 65 | + |
| 66 | + // Get database & collection access |
| 67 | + defaultDBAccess, err := u.GetDatabaseAccess(ctx, db) |
| 68 | + if err != nil { |
| 69 | + t.Fatalf("GetDatabaseAccess failed: %s", describe(err)) |
| 70 | + } |
| 71 | + defaultColAccess, err := u.GetCollectionAccess(ctx, col) |
| 72 | + if err != nil { |
| 73 | + t.Fatalf("GetCollectionAccess failed: %s", describe(err)) |
| 74 | + } |
| 75 | + |
| 76 | + // Get database & collection access using WithConfigured |
| 77 | + if grant, err := u.GetDatabaseAccess(driver.WithConfigured(ctx), db); err != nil { |
| 78 | + t.Fatalf("GetDatabaseAccess(WithConfigured) failed: %s", describe(err)) |
| 79 | + } else if grant != defaultDBAccess { |
| 80 | + t.Errorf("Database access using WithConfigured differs, got '%s', expected '%s'", grant, defaultDBAccess) |
| 81 | + } |
| 82 | + if grant, err := u.GetCollectionAccess(driver.WithConfigured(ctx), col); err != nil { |
| 83 | + t.Fatalf("GetCollectionAccess(WithConfigured) failed: %s", describe(err)) |
| 84 | + } else if grant != defaultDBAccess { |
| 85 | + t.Errorf("Collection access using WithConfigured differs, got '%s', expected '%s'", grant, defaultColAccess) |
| 86 | + } |
| 87 | + |
| 88 | + // Change server mode to readonly. |
| 89 | + if err := c.SetServerMode(ctx, driver.ServerModeReadOnly); err != nil { |
| 90 | + t.Fatalf("SetServerMode failed: %s", describe(err)) |
| 91 | + } |
| 92 | + |
| 93 | + // Check server mode, must be readonly |
| 94 | + if mode, err := c.ServerMode(ctx); err != nil { |
| 95 | + t.Fatalf("ServerMode failed: %s", describe(err)) |
| 96 | + } else if mode != driver.ServerModeReadOnly { |
| 97 | + t.Errorf("ServerMode returned '%s', but expected '%s'", mode, driver.ServerModeReadOnly) |
| 98 | + } |
| 99 | + |
| 100 | + // Get database & collection access now (must be readonly) |
| 101 | + if grant, err := u.GetDatabaseAccess(ctx, db); err != nil { |
| 102 | + t.Fatalf("GetDatabaseAccess failed: %s", describe(err)) |
| 103 | + } else if grant != driver.GrantReadOnly { |
| 104 | + t.Errorf("Database access must be readonly, got '%s'", grant) |
| 105 | + } |
| 106 | + if grant, err := u.GetCollectionAccess(ctx, col); err != nil { |
| 107 | + t.Fatalf("GetCollectionAccess failed: %s", describe(err)) |
| 108 | + } else if grant != driver.GrantReadOnly { |
| 109 | + t.Errorf("Collection access must be readonly, got '%s'", grant) |
| 110 | + } |
| 111 | + |
| 112 | + // Get database & collection access using WithConfigured (must be same as before) |
| 113 | + if grant, err := u.GetDatabaseAccess(driver.WithConfigured(ctx), db); err != nil { |
| 114 | + t.Fatalf("GetDatabaseAccess(WithConfigured) failed: %s", describe(err)) |
| 115 | + } else if grant != defaultDBAccess { |
| 116 | + t.Errorf("Database access using WithConfigured differs, got '%s', expected '%s'", grant, defaultDBAccess) |
| 117 | + } |
| 118 | + if grant, err := u.GetCollectionAccess(driver.WithConfigured(ctx), col); err != nil { |
| 119 | + t.Fatalf("GetCollectionAccess(WithConfigured) failed: %s", describe(err)) |
| 120 | + } else if grant != defaultDBAccess { |
| 121 | + t.Errorf("Collection access using WithConfigured differs, got '%s', expected '%s'", grant, defaultColAccess) |
| 122 | + } |
| 123 | + |
| 124 | + // Change server mode back to default. |
| 125 | + if err := c.SetServerMode(ctx, driver.ServerModeDefault); err != nil { |
| 126 | + t.Fatalf("SetServerMode failed: %s", describe(err)) |
| 127 | + } |
| 128 | + |
| 129 | + // Initial server mode must be default |
| 130 | + if mode, err := c.ServerMode(ctx); err != nil { |
| 131 | + t.Fatalf("ServerMode failed: %s", describe(err)) |
| 132 | + } else if mode != driver.ServerModeDefault { |
| 133 | + t.Errorf("ServerMode returned '%s', but expected '%s'", mode, driver.ServerModeDefault) |
| 134 | + } |
| 135 | + |
| 136 | + // Get database & collection access (must now be same as before) |
| 137 | + if grant, err := u.GetDatabaseAccess(ctx, db); err != nil { |
| 138 | + t.Fatalf("GetDatabaseAccess failed: %s", describe(err)) |
| 139 | + } else if grant != defaultDBAccess { |
| 140 | + t.Errorf("Database access differs, got '%s', expected '%s'", grant, defaultDBAccess) |
| 141 | + } |
| 142 | + if grant, err := u.GetCollectionAccess(ctx, col); err != nil { |
| 143 | + t.Fatalf("GetCollectionAccess failed: %s", describe(err)) |
| 144 | + } else if grant != defaultDBAccess { |
| 145 | + t.Errorf("Collection access differs, got '%s', expected '%s'", grant, defaultColAccess) |
| 146 | + } |
| 147 | + |
| 148 | + // Get database & collection access with WithConfigured (must now be same as before) |
| 149 | + if grant, err := u.GetDatabaseAccess(driver.WithConfigured(ctx), db); err != nil { |
| 150 | + t.Fatalf("GetDatabaseAccess(WithConfigured) failed: %s", describe(err)) |
| 151 | + } else if grant != defaultDBAccess { |
| 152 | + t.Errorf("Database access using WithConfigured differs, got '%s', expected '%s'", grant, defaultDBAccess) |
| 153 | + } |
| 154 | + if grant, err := u.GetCollectionAccess(driver.WithConfigured(ctx), col); err != nil { |
| 155 | + t.Fatalf("GetCollectionAccess(WithConfigured) failed: %s", describe(err)) |
| 156 | + } else if grant != defaultDBAccess { |
| 157 | + t.Errorf("Collection access using WithConfigured differs, got '%s', expected '%s'", grant, defaultColAccess) |
| 158 | + } |
| 159 | + } |
| 160 | +} |
0 commit comments