<commit_before>package models

import (
	"log"
	"testing"
	"time"

	"github.com/EaseApp/web-backend/src/db"
	r "github.com/dancannon/gorethink"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestNewUser(t *testing.T) {
	user, err := NewUser("user", "pass")
	require.NoError(t, err)
	assert.Equal(t, "user", user.Username)
	assert.NotEqual(t, "pass", user.PasswordHash)
	assert.NotEmpty(t, user.PasswordHash)
	assert.NotEmpty(t, user.APIToken)
	assert.NotEmpty(t, user.LoginToken)
	assert.WithinDuration(t, time.Now(), user.CreatedAt, time.Minute)
}

var localDBAddr = "localhost:28015"

func TestSaveAndFindUser(t *testing.T) {
	user, err := NewUser("user", "pass")
	require.NoError(t, err)
	querier := getUserQuerier(t)
	savedUser, err := querier.Save(user)
	require.NoError(t, err)
	assert.NotEmpty(t, savedUser.ID)

	foundUser := querier.Find("user")
	require.NotNil(t, foundUser)
	assertUsersEqual(t, savedUser, foundUser)
	assertUsersEqual(t, user, foundUser)
}

func TestAttemptLogin_Success(t *testing.T) {
	user, err := NewUser("user", "pass")
	require.NoError(t, err)
	querier := getUserQuerier(t)
	savedUser, err := querier.Save(user)
	require.NoError(t, err)

	loggedInUser, err := querier.AttemptLogin("user", "pass")
	require.NoError(t, err)
	assertUsersEqual(t, savedUser, loggedInUser)
}

func TestAttemptLogin_Fail(t *testing.T) {
	user, err := NewUser("user", "<PASSWORD>")
	require.NoError(t, err)
	querier := getUserQuerier(t)
	_, err = querier.Save(user)
	require.NoError(t, err)

	loggedInUser, err := querier.AttemptLogin("user", "<PASSWORD>")
	assert.Equal(t, "Password was invalid", err.Error())
	assert.Nil(t, loggedInUser)
}

func assertUsersEqual(t *testing.T, u1, u2 *User) {
	assert.Equal(t, u1.ID, u2.ID)
	assert.Equal(t, u1.Username, u2.Username)
	assert.Equal(t, u1.PasswordHash, u2.PasswordHash)
	assert.Equal(t, u1.APIToken, u2.APIToken)
	assert.Equal(t, u1.LoginToken, u2.LoginToken)
	assert.WithinDuration(t, u1.CreatedAt, u2.CreatedAt, time.Second)
}

func getUserQuerier(t *testing.T) *UserQuerier {
	client := getDBClient(t)
	return NewUserQuerier(client.Session)
}

func getDBClient(t *testing.T) *db.Client {
	client, err := db.NewClient(localDBAddr)
	require.NoError(t, err)

	// Clear the user table for the tests.
	res, err := r.DB("test").Table("users").Status().Run(client.Session)
	log.Info(res)
	log.Info(err)
	r.Wait(r.WaitOpts{WaitFor: "all_replicas_ready"}).Exec(client.Session)
	r.Wait(r.WaitOpts{WaitFor: "ready_for_writes"}).Exec(client.Session)
	r.DB("test").Table("users").Delete().Exec(client.Session)
	r.Wait(r.WaitOpts{WaitFor: "ready_for_writes"}).Exec(client.Session)
	r.Wait(r.WaitOpts{WaitFor: "all_replicas_ready"}).Exec(client.Session)
	r.DB("test").Table("users").Insert(map[string]string{"hello": "world"}).RunWrite(client.Session)
	_, err = r.DB("test").Table("users").Insert(struct{ prop string }{prop: "I am a string."}).RunWrite(client.Session)
	require.NoError(t, err)
	r.DB("test").Table("users").Insert(User{Username: "hi", CreatedAt: time.Now()}).RunWrite(client.Session)
	log.Println("DID STUFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF")
	t.Log("I DID THE USER STUFF AND DID NOT FAIL.")
	return client
}<commit_msg>print table<commit_after>package models

import (
	"log"
	"testing"
	"time"

	"github.com/EaseApp/web-backend/src/db"
	r "github.com/dancannon/gorethink"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestNewUser(t *testing.T) {
	user, err := NewUser("user", "pass")
	require.NoError(t, err)
	assert.Equal(t, "user", user.Username)
	assert.NotEqual(t, "pass", user.PasswordHash)
	assert.NotEmpty(t, user.PasswordHash)
	assert.NotEmpty(t, user.APIToken)
	assert.NotEmpty(t, user.LoginToken)
	assert.WithinDuration(t, time.Now(), user.CreatedAt, time.Minute)
}

var localDBAddr = "localhost:28015"

func TestSaveAndFindUser(t *testing.T) {
	user, err := NewUser("user", "pass")
	require.NoError(t, err)
	querier := getUserQuerier(t)
	savedUser, err := querier.Save(user)
	require.NoError(t, err)
	assert.NotEmpty(t, savedUser.ID)

	foundUser := querier.Find("user")
	require.NotNil(t, foundUser)
	assertUsersEqual(t, savedUser, foundUser)
	assertUsersEqual(t, user, foundUser)
}

func TestAttemptLogin_Success(t *testing.T) {
	user, err := NewUser("user", "pass")
	require.NoError(t, err)
	querier := getUserQuerier(t)
	savedUser, err := querier.Save(user)
	require.NoError(t, err)

	loggedInUser, err := querier.AttemptLogin("user", "pass")
	require.NoError(t, err)
	assertUsersEqual(t, savedUser, loggedInUser)
}

func TestAttemptLogin_Fail(t *testing.T) {
	user, err := NewUser("user", "pass")
	require.NoError(t, err)
	querier := getUserQuerier(t)
	_, err = querier.Save(user)
	require.NoError(t, err)

	loggedInUser, err := querier.AttemptLogin("user", "badpass")
	assert.Equal(t, "Password was invalid", err.Error())
	assert.Nil(t, loggedInUser)
}

func assertUsersEqual(t *testing.T, u1, u2 *User) {
	assert.Equal(t, u1.ID, u2.ID)
	assert.Equal(t, u1.Username, u2.Username)
	assert.Equal(t, u1.PasswordHash, u2.PasswordHash)
	assert.Equal(t, u1.APIToken, u2.APIToken)
	assert.Equal(t, u1.LoginToken, u2.LoginToken)
	assert.WithinDuration(t, u1.CreatedAt, u2.CreatedAt, time.Second)
}

func getUserQuerier(t *testing.T) *UserQuerier {
	client := getDBClient(t)
	return NewUserQuerier(client.Session)
}

func getDBClient(t *testing.T) *db.Client {
	client, err := db.NewClient(localDBAddr)
	require.NoError(t, err)

	// Clear the user table for the tests.
	res, err := r.DB("test").Table("users").Status().Run(client.Session)
	log.Println(res)
	log.Println(err)
	r.Wait(r.WaitOpts{WaitFor: "all_replicas_ready"}).Exec(client.Session)
	r.Wait(r.WaitOpts{WaitFor: "ready_for_writes"}).Exec(client.Session)
	r.DB("test").Table("users").Delete().Exec(client.Session)
	r.Wait(r.WaitOpts{WaitFor: "ready_for_writes"}).Exec(client.Session)
	r.Wait(r.WaitOpts{WaitFor: "all_replicas_ready"}).Exec(client.Session)
	r.DB("test").Table("users").Insert(map[string]string{"hello": "world"}).RunWrite(client.Session)
	_, err = r.DB("test").Table("users").Insert(struct{ prop string }{prop: "I am a string."}).RunWrite(client.Session)
	require.NoError(t, err)
	r.DB("test").Table("users").Insert(User{Username: "hi", CreatedAt: time.Now()}).RunWrite(client.Session)
	log.Println("DID STUFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF")
	t.Log("I DID THE USER STUFF AND DID NOT FAIL.")
	return client
}
{'fraction_non_alphanumeric': 0.1708262284917067, 'fraction_numerical': 0.0058905596031623006, 'mean_word_length': 5.982683982683983, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '93458', 'n_tokens_mistral': 2523, 'n_tokens_neox': 2481, 'n_words': 423}