<commit_before>package be

import (
	"testing"

	. "github.com/chai2010/assert"
	"github.com/coocood/qbs"
)

func TestUserAPI(t *testing.T) {
	CreateAndInitDB()
	db, err := qbs.GetQbs()
	AssertNil(t, err)
	defer func() {
		WipeDB()
		db.Close()
	}()

	testApi, err := NewTestAPI()
	AssertNil(t, err)
	defer testApi.Stop()

	users, err := FindUsers(0, 100, db)
	AssertNil(t, err)
	AssertEqual(t, 0, len(users), "Need to have 0 users when starting")

	user, err := CreateUser("<EMAIL>", "Adrian", "Monk", false, db)
	AssertNil(t, err)
	_, err = CreatePassword("<PASSWORD>", user.Id, db)
	AssertNil(t, err)
	staff, err := CreateUser("<EMAIL>", "Sherona", "Smith", true, db)
	AssertNil(t, err)
	_, err = CreatePassword("<PASSWORD>", staff.Id, db)
	AssertNil(t, err)

	Assert403(t, "GET", testApi.URL()+"/user/")
	Assert403(t, "GET", testApi.URL()+"/user/"+user.UUID)

	userClient, err := NewClient(testApi.URL())
	AssertNil(t, err)
	err = userClient.Authenticate(user.Email, "")
	AssertNotNil(t, err, "Should have failed with empty password")
	err = userClient.Authenticate("", "1234")
	AssertNotNil(t, err, "Should have failed with empty email")
	err = userClient.Authenticate("", "")
	AssertNotNil(t, err, "Should have failed with empty login info")
	err = userClient.Authenticate(user.Email, "4321")
	AssertNotNil(t, err, "Should have failed with incorrect password")
	err = userClient.Authenticate(user.Email, "1234")
	AssertNil(t, err, "Should have authenticated with proper email and username")

	user2 := new(User)
	err = userClient.GetJSON("/user/current", user2)
	AssertNil(t, err, "Error fetching current user")
	AssertEqual(t, user.Id, user2.Id)
	_, err = userClient.GetList("/user/")
	AssertNotNil(t, err, "Users API should be staff only")
	err = userClient.GetJSON("/user/"+user2.UUID, user2)
	AssertNotNil(t, err, "User API should be staff only")

	staffClient, err := NewClient(testApi.URL())
	AssertNil(t, err)
	err = staffClient.Authenticate(staff.Email, "1234")
	AssertNil(t, err)
	err = staffClient.GetJSON("/user/"+user2.UUID, user2)
	AssertNil(t, err, "API should be readable by staff")
	AssertEqual(t, user.Id, user2.Id)

	list, err := staffClient.GetList("/user/")
	AssertNil(t, err)
	arr := list.Objects.([]interface{})
	AssertEqual(t, 2, len(arr))

	// Test that staff can update a User
	staff2 := new(User)
	err = staffClient.GetJSON("/user/current", staff2)
	AssertNil(t, err)
	staff2.FirstName = "Pickles"
	staff2.LastName = "McGee"
	err = staffClient.UpdateUser(staff2)
	AssertNil(t, err)
	AssertEqual(t, staff2.FirstName, "Pickles")
	AssertEqual(t, staff2.LastName, "McGee")
	staff3 := new(User)
	err = staffClient.GetJSON("/user/current", staff3)
	AssertNil(t, err)
	AssertEqual(t, staff2.FirstName, staff3.FirstName)
	AssertEqual(t, staff2.LastName, staff3.LastName)
}

func TestUser(t *testing.T) {
	CreateAndInitDB()
	db, err := qbs.GetQbs()
	AssertNil(t, err)
	defer func() {
		WipeDB()
		db.Close()
	}()

	user, err := CreateUser("<EMAIL>", "Adrian", "Monk", false, db)
	AssertNil(t, err)
	AssertNotEqual(t, user.UUID, "")

	_, err = FindUser("not-a-uuid", db)
	AssertNotNil(t, err)

	user2, err := FindUser(user.UUID, db)
	AssertNil(t, err)
	AssertEqual(t, user2.UUID, user.UUID)
	AssertEqual(t, user2.Email, user.Email)

	user2.Email = "<EMAIL>"
	err = UpdateUser(user2, db)
	AssertNil(t, err)
	AssertEqual(t, user2.UUID, user.UUID)
	user3, err := FindUser(user2.UUID, db)
	AssertNil(t, err)
	AssertEqual(t, user2.Email, user3.Email)

	// TODO
	/*
		Test schema API
		Test versioning enforcement
	*/
}<commit_msg>Furthered testing for user and schema resources.<commit_after>package be

import (
	"testing"

	. "github.com/chai2010/assert"
	"github.com/coocood/qbs"
)

func TestUserAPI(t *testing.T) {
	CreateAndInitDB()
	db, err := qbs.GetQbs()
	AssertNil(t, err)
	defer func() {
		WipeDB()
		db.Close()
	}()

	testApi, err := NewTestAPI()
	AssertNil(t, err)
	defer testApi.Stop()

	users, err := FindUsers(0, 100, db)
	AssertNil(t, err)
	AssertEqual(t, 0, len(users), "Need to have 0 users when starting")

	user, err := CreateUser("<EMAIL>", "Adrian", "Monk", false, db)
	AssertNil(t, err)
	_, err = CreatePassword("<PASSWORD>", user.Id, db)
	AssertNil(t, err)
	staff, err := CreateUser("<EMAIL>", "Sherona", "Smith", true, db)
	AssertNil(t, err)
	_, err = CreatePassword("<PASSWORD>", staff.Id, db)
	AssertNil(t, err)

	Assert403(t, "GET", testApi.URL()+"/user/")
	Assert403(t, "GET", testApi.URL()+"/user/"+user.UUID)

	userClient, err := NewClient(testApi.URL())
	AssertNil(t, err)
	err = userClient.Authenticate(user.Email, "")
	AssertNotNil(t, err, "Should have failed with empty password")
	err = userClient.Authenticate("", "1234")
	AssertNotNil(t, err, "Should have failed with empty email")
	err = userClient.Authenticate("", "")
	AssertNotNil(t, err, "Should have failed with empty login info")
	err = userClient.Authenticate(user.Email, "4321")
	AssertNotNil(t, err, "Should have failed with incorrect password")
	err = userClient.Authenticate(user.Email, "1234")
	AssertNil(t, err, "Should have authenticated with proper email and username")

	user2 := new(User)
	err = userClient.GetJSON("/user/current", user2)
	AssertNil(t, err, "Error fetching current user")
	AssertEqual(t, user.Id, user2.Id)
	_, err = userClient.GetList("/user/")
	AssertNotNil(t, err, "Users API should be staff only")
	err = userClient.GetJSON("/user/"+user2.UUID, user2)
	AssertNotNil(t, err, "User API should be staff only")

	staffClient, err := NewClient(testApi.URL())
	AssertNil(t, err)
	err = staffClient.Authenticate(staff.Email, "1234")
	AssertNil(t, err)
	err = staffClient.GetJSON("/user/"+user2.UUID, user2)
	AssertNil(t, err, "API should be readable by staff")
	AssertEqual(t, user.Id, user2.Id)

	list, err := staffClient.GetList("/user/")
	AssertNil(t, err)
	arr := list.Objects.([]interface{})
	AssertEqual(t, 2, len(arr))

	// Test that staff can update a User
	staff2 := new(User)
	err = staffClient.GetJSON("/user/current", staff2)
	AssertNil(t, err)
	staff2.FirstName = "Pickles"
	staff2.LastName = "McGee"
	err = staffClient.UpdateUser(staff2)
	AssertNil(t, err)
	AssertEqual(t, staff2.FirstName, "Pickles")
	AssertEqual(t, staff2.LastName, "McGee")
	staff3 := new(User)
	err = staffClient.GetJSON("/user/current", staff3)
	AssertNil(t, err)
	AssertEqual(t, staff2.FirstName, staff3.FirstName)
	AssertEqual(t, staff2.LastName, staff3.LastName)
}

func TestUser(t *testing.T) {
	CreateAndInitDB()
	db, err := qbs.GetQbs()
	AssertNil(t, err)
	defer func() {
		WipeDB()
		db.Close()
	}()

	user, err := CreateUser("<EMAIL>", "Adrian", "Monk", false, db)
	AssertNil(t, err)
	AssertNotEqual(t, user.UUID, "")

	_, err = FindUser("not-a-uuid", db)
	AssertNotNil(t, err)

	user2, err := FindUser(user.UUID, db)
	AssertNil(t, err)
	AssertEqual(t, user2.UUID, user.UUID)
	AssertEqual(t, user2.Email, user.Email)

	user2.Email = "<EMAIL>"
	err = UpdateUser(user2, db)
	AssertNil(t, err)
	AssertEqual(t, user2.UUID, user.UUID)
	user3, err := FindUser(user2.UUID, db)
	AssertNil(t, err)
	AssertEqual(t, user2.Email, user3.Email)

	// TODO
	/*
		Test versioning enforcement
	*/
}
{'fraction_non_alphanumeric': 0.16692448319504993, 'fraction_numerical': 0.0180002812543946, 'mean_word_length': 5.00168776371308, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '7546616', 'n_tokens_mistral': 2782, 'n_tokens_neox': 2699, 'n_words': 637}