2023-09-19 22:01:41 +00:00
|
|
|
package scsusers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/jmoiron/sqlx"
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestUsers(t *testing.T) {
|
|
|
|
var email string
|
2023-09-19 22:09:33 +00:00
|
|
|
c.testing = true
|
2023-09-19 22:01:41 +00:00
|
|
|
flag.StringVar(&email, "email", "", "Email address to use for registration test")
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
db, err := sqlx.Open("sqlite3", ":memory:")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Couldn't open sqlite3 in-memory db:" + err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = db.Ping()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Couldn't ping sqlite3 in-memory db:" + err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
schema := `CREATE TABLE test_auth (
|
|
|
|
id integer primary key autoincrement,
|
|
|
|
username text NOT NULL unique,
|
|
|
|
password text NOT NULL);`
|
|
|
|
_, err = db.Exec(schema)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Schema creation failed: " + err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
Init(db, "test", "Example Test", "nobody@nowhere.com", "localhost:25")
|
|
|
|
a := UsernameAvailable("testuser")
|
|
|
|
fmt.Printf("Initial test of username available: %v\n", a)
|
|
|
|
a = Register("testuser", email, "127.0.0.1")
|
|
|
|
fmt.Printf("Register returned %v\n", a)
|
|
|
|
fmt.Printf("Attempt to log in with invalid username returned %v\n", Login("baduser", "badpass"))
|
|
|
|
fmt.Printf("Enter code from email:")
|
|
|
|
var code string
|
|
|
|
fmt.Scan(&code)
|
|
|
|
a = Login("testuser", code)
|
|
|
|
fmt.Printf("Login returned %v\n", a)
|
|
|
|
|
|
|
|
}
|