<commit_before>//
// DISCLAIMER
//
// Copyright 2017 ArangoDB GmbH, Cologne, Germany
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Copyright holder is ArangoDB GmbH, Cologne, Germany
//
// Author <NAME>
//

// +build auth

package test

import (
	"context"
	"testing"

	driver "github.com/arangodb/go-driver"
)

// TestUpdateUserPasswordMyself creates a user and tries to update the password of the authenticated user.
func TestUpdateUserPasswordMyself(t *testing.T) {
	var conn driver.Connection
	c := createClientFromEnv(t, true, &conn)
	version, err := c.Version(nil)
	if err != nil {
		t.Fatalf("Version failed: %s", describe(err))
	}
	isv32p := version.Version.CompareTo("3.2") >= 0
	isVST1_0 := conn.Protocols().Contains(driver.ProtocolVST1_0)
	ensureUser(nil, c, "user@TestUpdateUserPasswordMyself", &driver.UserOptions{Password: "<PASSWORD>"}, t)

	authClient, err := driver.NewClient(driver.ClientConfig{
		Connection:     createConnectionFromEnv(t),
		Authentication: driver.BasicAuthentication("user@TestUpdateUserPasswordMyself", "foo"),
	})
	if err != nil {
		t.Fatalf("Expected success, got %s", describe(err))
	}

	if isVST1_0 && !isv32p {
		t.Skip("Cannot update my own password using VST in 3.1")
	} else {
		u, err := authClient.User(nil, "user@TestUpdateUserPasswordMyself")
		if err != nil {
			t.Fatalf("Expected success, got %s", describe(err))
		}
		if err := u.Update(context.TODO(), driver.UserOptions{Password: "<PASSWORD>"}); err != nil {
			t.Errorf("Expected success, got %s", describe(err))
		}
	}
}

// TestUpdateUserPasswordOtherUser creates a user and tries to update the password of another user.
func TestUpdateUserPasswordOtherUser(t *testing.T) {
	var conn driver.Connection
	c := createClientFromEnv(t, true, &conn)
	version, err := c.Version(nil)
	if err != nil {
		t.Fatalf("Version failed: %s", describe(err))
	}
	isv32p := version.Version.CompareTo("3.2") >= 0
	isVST1_0 := conn.Protocols().Contains(driver.ProtocolVST1_0)
	u1 := ensureUser(nil, c, "user1", &driver.UserOptions{Password: "<PASSWORD>"}, t)
	ensureUser(nil, c, "user2", nil, t)
	systemDb, err := c.Database(nil, "_system")
	if err != nil {
		t.Fatalf("Expected success, got %s", describe(err))
	}

	authClient, err := driver.NewClient(driver.ClientConfig{
		Connection:     createConnectionFromEnv(t),
		Authentication: driver.BasicAuthentication("user1", "foo"),
	})
	if err != nil {
		t.Fatalf("Expected success, got %s", describe(err))
	}

	if isVST1_0 && !isv32p {
		t.Skip("Cannot update other password using VST in 3.1")
	} else {
		// Right now user1 has no right to access user2
		if _, err := authClient.User(nil, "user2"); !driver.IsForbidden(err) {
			t.Fatalf("Expected ForbiddenError, got %s", describe(err))
		}

		// Grant user1 access to _system db, then it should be able to access user2
		if err := u1.SetDatabaseAccess(nil, systemDb, driver.GrantReadWrite); err != nil {
			t.Fatalf("Expected success, got %s", describe(err))
		}

		// Now change the password of another user.
		// With user1 having rights for _system, this must succeed now
		u2, err := authClient.User(nil, "user2")
		if err != nil {
			t.Fatalf("Expected success, got %s", describe(err))
		}
		if err := u2.Update(context.TODO(), driver.UserOptions{Password: "<PASSWORD>"}); err != nil {
			t.Errorf("Expected success, got %s", describe(err))
		}
	}
}

// TestGrantUserDatabase creates a user & database and granting the user access to the database.
func TestGrantUserDatabase(t *testing.T) {
	c := createClientFromEnv(t, true)
	version, err := c.Version(nil)
	if err != nil {
		t.Fatalf("Version failed: %s", describe(err))
	}
	isv32p := version.Version.CompareTo("3.2") >= 0
	u := ensureUser(nil, c, "grant_user1", &driver.UserOptions{Password: "<PASSWORD>"}, t)
	db := ensureDatabase(nil, c, "grant_user_test", nil, t)

	// Grant read/write access
	if err := u.SetDatabaseAccess(nil, db, driver.GrantReadWrite); err != nil {
		t.Fatalf("SetDatabaseAccess failed: %s", describe(err))
	}
	// Read back access
	if grant, err := u.GetDatabaseAccess(nil, db); err != nil {
		t.Fatalf("GetDatabaseAccess failed: %s", describe(err))
	} else if grant != driver.GrantReadWrite {
		t.Errorf("Database access invalid, expected 'rw', got '%s'", grant)
	}

	authClient, err := driver.NewClient(driver.ClientConfig{
		Connection:     createConnectionFromEnv(t),
		Authentication: driver.BasicAuthentication("grant_user1", "foo"),
	})
	if err != nil {
		t.Fatalf("Expected success, got %s", describe(err))
	}

	// Try to create a collection in the db
	authDb, err := authClient.Database(nil, "grant_user_test")
	if err != nil {
		t.Fatalf("Expected success, got %s", describe(err))
	}
	if _, err := authDb.CreateCollection(nil, "some_collection", nil); err != nil {
		t.Errorf("Expected success, got %s", describe(err))
	}

	// Now revoke access
	if err := u.SetDatabaseAccess(nil, db, driver.GrantNone); err != nil {
		t.Fatalf("SetDatabaseAccess failed: %s", describe(err))
	}
	// Read back access
	if grant, err := u.GetDatabaseAccess(nil, db); err != nil {
		t.Fatalf("GetDatabaseAccess failed: %s", describe(err))
	} else if grant != driver.GrantNone {
		t.Errorf("Database access invalid, expected 'none', got '%s'", grant)<commit_msg>Fixed tests for 3.1<commit_after>//
// DISCLAIMER
//
// Copyright 2017 ArangoDB GmbH, Cologne, Germany
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Copyright holder is ArangoDB GmbH, Cologne, Germany
//
// Author <NAME>
//

// +build auth

package test

import (
	"context"
	"testing"

	driver "github.com/arangodb/go-driver"
)

// TestUpdateUserPasswordMyself creates a user and tries to update the password of the authenticated user.
func TestUpdateUserPasswordMyself(t *testing.T) {
	var conn driver.Connection
	c := createClientFromEnv(t, true, &conn)
	version, err := c.Version(nil)
	if err != nil {
		t.Fatalf("Version failed: %s", describe(err))
	}
	isv32p := version.Version.CompareTo("3.2") >= 0
	isVST1_0 := conn.Protocols().Contains(driver.ProtocolVST1_0)
	ensureUser(nil, c, "user@TestUpdateUserPasswordMyself", &driver.UserOptions{Password: "<PASSWORD>"}, t)

	authClient, err := driver.NewClient(driver.ClientConfig{
		Connection:     createConnectionFromEnv(t),
		Authentication: driver.BasicAuthentication("user@TestUpdateUserPasswordMyself", "foo"),
	})
	if err != nil {
		t.Fatalf("Expected success, got %s", describe(err))
	}

	if isVST1_0 && !isv32p {
		t.Skip("Cannot update my own password using VST in 3.1")
	} else {
		u, err := authClient.User(nil, "user@TestUpdateUserPasswordMyself")
		if err != nil {
			t.Fatalf("Expected success, got %s", describe(err))
		}
		if err := u.Update(context.TODO(), driver.UserOptions{Password: "<PASSWORD>"}); err != nil {
			t.Errorf("Expected success, got %s", describe(err))
		}
	}
}

// TestUpdateUserPasswordOtherUser creates a user and tries to update the password of another user.
func TestUpdateUserPasswordOtherUser(t *testing.T) {
	var conn driver.Connection
	c := createClientFromEnv(t, true, &conn)
	version, err := c.Version(nil)
	if err != nil {
		t.Fatalf("Version failed: %s", describe(err))
	}
	isv32p := version.Version.CompareTo("3.2") >= 0
	isVST1_0 := conn.Protocols().Contains(driver.ProtocolVST1_0)
	u1 := ensureUser(nil, c, "user1", &driver.UserOptions{Password: "<PASSWORD>"}, t)
	ensureUser(nil, c, "user2", nil, t)
	systemDb, err := c.Database(nil, "_system")
	if err != nil {
		t.Fatalf("Expected success, got %s", describe(err))
	}

	authClient, err := driver.NewClient(driver.ClientConfig{
		Connection:     createConnectionFromEnv(t),
		Authentication: driver.BasicAuthentication("user1", "foo"),
	})
	if err != nil {
		t.Fatalf("Expected success, got %s", describe(err))
	}

	if isVST1_0 && !isv32p {
		t.Skip("Cannot update other password using VST in 3.1")
	} else {
		// Right now user1 has no right to access user2
		if _, err := authClient.User(nil, "user2"); !driver.IsForbidden(err) {
			t.Fatalf("Expected ForbiddenError, got %s", describe(err))
		}

		// Grant user1 access to _system db, then it should be able to access user2
		if err := u1.SetDatabaseAccess(nil, systemDb, driver.GrantReadWrite); err != nil {
			t.Fatalf("Expected success, got %s", describe(err))
		}

		// Now change the password of another user.
		// With user1 having rights for _system, this must succeed now
		u2, err := authClient.User(nil, "user2")
		if err != nil {
			t.Fatalf("Expected success, got %s", describe(err))
		}
		if err := u2.Update(context.TODO(), driver.UserOptions{Password: "<PASSWORD>"}); err != nil {
			t.Errorf("Expected success, got %s", describe(err))
		}
	}
}

// TestGrantUserDatabase creates a user & database and granting the user access to the database.
func TestGrantUserDatabase(t *testing.T) {
	c := createClientFromEnv(t, true)
	version, err := c.Version(nil)
	if err != nil {
		t.Fatalf("Version failed: %s", describe(err))
	}
	isv32p := version.Version.CompareTo("3.2") >= 0
	u := ensureUser(nil, c, "grant_user1", &driver.UserOptions{Password: "<PASSWORD>"}, t)
	db := ensureDatabase(nil, c, "grant_user_test", nil, t)

	// Grant read/write access
	if err := u.SetDatabaseAccess(nil, db, driver.GrantReadWrite); err != nil {
		t.Fatalf("SetDatabaseAccess failed: %s", describe(err))
	}
	if isv32p {
		// Read back access
		if grant, err := u.GetDatabaseAccess(nil, db); err != nil {
			t.Fatalf("GetDatabaseAccess failed: %s", describe(err))
		} else if grant != driver.GrantReadWrite {
			t.Errorf("Database access invalid, expected 'rw', got '%s'", grant)
		}
	}

	authClient, err := driver.NewClient(driver.ClientConfig{
		Connection:     createConnectionFromEnv(t),
		Authentication: driver.BasicAuthentication("grant_user1", "foo"),
	})
	if err != nil {
		t.Fatalf("Expected success, got %s", describe(err))
	}

	// Try to create a collection in the db
	authDb, err := authClient.Database(nil, "grant_user_test")
	if err != nil {
		t.Fatalf("Expected success, got %s", describe(err))
	}
	if _, err := authDb.CreateCollection(nil, "some_collection", nil); err != nil {
		t.Errorf("Expected success, got %s", describe(err))
	}

	// Now revoke access
	if err := u.SetDatabaseAccess(nil, db, driver.GrantNone); err != nil {
		t.Fatalf("SetDatabaseAccess failed: %s", describe(err))
	}
	if isv32p {
		// Read back access
		if grant, err := u.GetDatabaseAccess(nil, db); err != nil {
			t.Fatalf("GetDatabaseAccess failed: %s", describe(err))
		} else if grant != driver.GrantNone {
			t.Errorf("Database access invalid, expected 'none', got '%s'", grant)
		}
{'fraction_non_alphanumeric': 0.14599236641221375, 'fraction_numerical': 0.010756419153365717, 'mean_word_length': 4.325173210161663, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2935903', 'n_tokens_mistral': 3941, 'n_tokens_neox': 3764, 'n_words': 1108}