<commit_before>package model

import (
	"reflect"
	"testing"
)

func mockUserTest() *User {
	return &User{
		Id:       1,
		Username: "test",
		Email:    "test",
		Password: []byte{},
		PasswordHash: []byte{0x24, 0x32, 0x61, 0x24, 0x31, 0x30,
			0x24, 0x72, 0x45, 0x73, 0x37, 0x75, 0x64, 0x69, 0x4b,
			0x77, 0x4b, 0x65, 0x46, 0x69, 0x4c, 0x63, 0x33, 0x49,
			0x68, 0x4d, 0x65, 0x63, 0x65, 0x51, 0x6c, 0x49, 0x68,
			0x4d, 0x46, 0x51, 0x4e, 0x70, 0x30, 0x6a, 0x79, 0x69,
			0x51, 0x78, 0x4d, 0x75, 0x77, 0x31, 0x51, 0x43, 0x36,
			0x37, 0x4f, 0x6e, 0x4f, 0x47, 0x6a, 0x63, 0x51, 0x75},
	}
}

func mockUserTester() *User {
	return &User{
		Id:       2,
		Username: "tester",
		Email:    "<EMAIL>",
		Password: []byte{},
		PasswordHash: []byte{0x24, 0x32, 0x61, 0x24, 0x31, 0x30,
			0x24, 0x55, 0x2f, 0x31, 0x58, 0x4e, 0x51, 0x67, 0x54,
			0x50, 0x54, 0x52, 0x6d, 0x37, 0x34, 0x6e, 0x45, 0x6c,
			0x49, 0x51, 0x47, 0x39, 0x75, 0x6b, 0x66, 0x6f, 0x79,
			0x6a, 0x4b, 0x75, 0x47, 0x2e, 0x6a, 0x55, 0x47, 0x37,
			0x65, 0x34, 0x58, 0x64, 0x48, 0x57, 0x33, 0x43, 0x70,
			0x64, 0x6b, 0x67, 0x65, 0x47, 0x51, 0x6c, 0x4a, 0x6d},
	}
}

func TestEmptyuser(t *testing.T) {
	if !reflect.DeepEqual(NewUser(), &User{-1, "", "", []byte{}, []byte{}}) {
		t.Error("user not empty")
	}
}

func TestFindOneUserByUsername(t *testing.T) {
	db, err := GetMockupDB()
	defer db.Close()
	if err != nil {
		t.Fatal(err)
	}

	userTest, err := FindOneUserByUsername(db, "test")
	if err != nil {
		t.Fatal(err)
	}
	if !reflect.DeepEqual(userTest, *mockUserTest()) {
		t.Error("wrong user")
	}

	userTester, err := FindOneUserByUsername(db, "tester")
	if err != nil {
		t.Fatal(err)
	}
	if !reflect.DeepEqual(userTester, *mockUserTester()) {
		t.Error("wrong user")
	}
}

func TestFindOneUserById(t *testing.T) {
	db, err := GetMockupDB()
	defer db.Close()
	if err != nil {
		t.Fatal(err)
	}

	userTest, err := FindOneUserById(db, 1)
	if err != nil {
		t.Fatal(err)
	}
	if !reflect.DeepEqual(userTest, *mockUserTest()) {
		t.Error("wrong user")
	}

	userTester, err := FindOneUserById(db, 2)
	if err != nil {
		t.Fatal(err)
	}
	if !reflect.DeepEqual(userTester, *mockUserTester()) {
		t.Error("wrong user")
	}
}

func TestSaveUser(t *testing.T) {
	db, err := GetMockupDB()
	defer db.Close()
	if err != nil {
		t.Fatal(err)
	}

	user := mockUserTest()
	user.PasswordHash = []byte{}

	err = SaveUser(db, user)
	if err == nil {
		t.Error("a user must have a hashed password")
	}

	user = mockUserTest()
	err = SaveUser(db, user)
	if err != nil {
		t.Fatal(err)
	}
	user.Id = 3
	userTest, err := FindOneUserById(db, 3)
	if err != nil {
		t.Fatal(err)
	}
	if !reflect.DeepEqual(userTest, *user) {
		t.Error("wrong user")
	}
}

func TestHashPassword(t *testing.T) {
	db, err := GetMockupDB()
	defer db.Close()
	if err != nil {
		t.Fatal(err)
	}

	user, err := FindOneUserById(db, 1)
	if err != nil {
		t.Fatal(err)
	}

	if !reflect.DeepEqual(*mockUserTest(), user) {
		t.Error("wrong user")
	}

	if len(user.Password) != 0 {
		t.Error("password not empty")
	}

	newPassword := []byte("<PASSWORD>")
	user.Password = <PASSWORD>
	user.PasswordHash = []byte{}
	user.HashPassword()

	for _, v := range user.Password {
		if v != 0 {
			t.Error("password should be cleared")
		}
	}

	if len(user.PasswordHash) <= 0 {
		t.Error("password hash should now be set")
	}
}

func TestCompareHashAndPassword(t *testing.T) {
	db, err := GetMockupDB()
	defer db.Close()
	if err != nil {
		t.Fatal(err)
	}

	user, err := FindOneUserById(db, 1)
	if err != nil {
		t.Fatal(err)
	}

	if !reflect.DeepEqual(*mockUserTest(), user) {
		t.Error("wrong user")
	}

	password := []byte("<PASSWORD>")
	if err := user.CompareHashAndPassword(&password); err != nil {
		t.Errorf("wrong password: %s", err)
	}

}<commit_msg>more tests<commit_after>package model

import (
	"reflect"
	"testing"
)

func mockUserTest() *User {
	return &User{
		Id:       1,
		Username: "test",
		Email:    "test",
		Password: []byte{},
		PasswordHash: []byte{0x24, 0x32, 0x61, 0x24, 0x31, 0x30,
			0x24, 0x72, 0x45, 0x73, 0x37, 0x75, 0x64, 0x69, 0x4b,
			0x77, 0x4b, 0x65, 0x46, 0x69, 0x4c, 0x63, 0x33, 0x49,
			0x68, 0x4d, 0x65, 0x63, 0x65, 0x51, 0x6c, 0x49, 0x68,
			0x4d, 0x46, 0x51, 0x4e, 0x70, 0x30, 0x6a, 0x79, 0x69,
			0x51, 0x78, 0x4d, 0x75, 0x77, 0x31, 0x51, 0x43, 0x36,
			0x37, 0x4f, 0x6e, 0x4f, 0x47, 0x6a, 0x63, 0x51, 0x75},
	}
}

func mockUserTester() *User {
	return &User{
		Id:       2,
		Username: "tester",
		Email:    "<EMAIL>",
		Password: []byte{},
		PasswordHash: []byte{0x24, 0x32, 0x61, 0x24, 0x31, 0x30,
			0x24, 0x55, 0x2f, 0x31, 0x58, 0x4e, 0x51, 0x67, 0x54,
			0x50, 0x54, 0x52, 0x6d, 0x37, 0x34, 0x6e, 0x45, 0x6c,
			0x49, 0x51, 0x47, 0x39, 0x75, 0x6b, 0x66, 0x6f, 0x79,
			0x6a, 0x4b, 0x75, 0x47, 0x2e, 0x6a, 0x55, 0x47, 0x37,
			0x65, 0x34, 0x58, 0x64, 0x48, 0x57, 0x33, 0x43, 0x70,
			0x64, 0x6b, 0x67, 0x65, 0x47, 0x51, 0x6c, 0x4a, 0x6d},
	}
}

func TestEmptyuser(t *testing.T) {
	if !reflect.DeepEqual(NewUser(), &User{-1, "", "", []byte{}, []byte{}}) {
		t.Error("user not empty")
	}
}

func TestFindOneUserByUsername(t *testing.T) {
	db, err := GetMockupDB()
	defer db.Close()
	if err != nil {
		t.Fatal(err)
	}

	userTest, err := FindOneUserByUsername(db, "test")
	if err != nil {
		t.Fatal(err)
	}
	if !reflect.DeepEqual(userTest, *mockUserTest()) {
		t.Error("wrong user")
	}

	userTester, err := FindOneUserByUsername(db, "tester")
	if err != nil {
		t.Fatal(err)
	}
	if !reflect.DeepEqual(userTester, *mockUserTester()) {
		t.Error("wrong user")
	}
}

func TestFindOneUserById(t *testing.T) {
	db, err := GetMockupDB()
	defer db.Close()
	if err != nil {
		t.Fatal(err)
	}

	userTest, err := FindOneUserById(db, 1)
	if err != nil {
		t.Fatal(err)
	}
	if !reflect.DeepEqual(userTest, *mockUserTest()) {
		t.Error("wrong user")
	}

	userTester, err := FindOneUserById(db, 2)
	if err != nil {
		t.Fatal(err)
	}
	if !reflect.DeepEqual(userTester, *mockUserTester()) {
		t.Error("wrong user")
	}
}

func TestSaveUser(t *testing.T) {
	db, err := GetMockupDB()
	defer db.Close()
	if err != nil {
		t.Fatal(err)
	}

	user := mockUserTest()
	user.PasswordHash = []byte{}

	err = SaveUser(db, user)
	if err == nil {
		t.Error("a user must have a hashed password")
	}

	user = mockUserTest()
	err = SaveUser(db, user)
	if err != nil {
		t.Fatal(err)
	}
	user.Id = 3
	userTest, err := FindOneUserById(db, 3)
	if err != nil {
		t.Fatal(err)
	}
	if !reflect.DeepEqual(userTest, *user) {
		t.Error("wrong user")
	}
}

func TestHashPassword(t *testing.T) {
	db, err := GetMockupDB()
	defer db.Close()
	if err != nil {
		t.Fatal(err)
	}

	user, err := FindOneUserById(db, 1)
	if err != nil {
		t.Fatal(err)
	}

	if !reflect.DeepEqual(*mockUserTest(), user) {
		t.Error("wrong user")
	}

	if len(user.Password) != 0 {
		t.Error("password not empty")
	}

	newPassword := []byte("<PASSWORD>")
	user.Password = <PASSWORD>
	user.PasswordHash = []byte{}
	user.HashPassword()

	for _, v := range user.Password {
		if v != 0 {
			t.Error("password should be cleared")
		}
	}

	if len(user.PasswordHash) <= 0 {
		t.Error("password hash should now be set")
	}
}

func TestCompareHashAndPassword(t *testing.T) {
	db, err := GetMockupDB()
	defer db.Close()
	if err != nil {
		t.Fatal(err)
	}

	user, err := FindOneUserById(db, 1)
	if err != nil {
		t.Fatal(err)
	}

	if !reflect.DeepEqual(*mockUserTest(), user) {
		t.Error("wrong user")
	}

	password := []byte("<PASSWORD>")
	if err := user.CompareHashAndPassword(&password); err != nil {
		t.Errorf("wrong password: %s", err)
	}

	for _, v := range password {
		if v != 0 {
			t.Error("password should be cleared")
		}
	}
}
{'fraction_non_alphanumeric': 0.1760757994472957, 'fraction_numerical': 0.09066982497697065, 'mean_word_length': 3.645476772616137, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 11, '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': '612241', 'n_tokens_mistral': 4098, 'n_tokens_neox': 3482, 'n_words': 786}