package userstore

import "testing"

func TestNormalizePhone(t *testing.T) {
	ok, err := NormalizePhone("+86 138-0013-8000")
	if err != nil || ok != "13800138000" {
		t.Fatalf("got %q %v", ok, err)
	}
	if _, err := NormalizePhone("12345"); err == nil {
		t.Fatal("expected error")
	}
	if !LooksLikePhone("13912345678") {
		t.Fatal("should look like phone")
	}
}

func TestLoginByPhone(t *testing.T) {
	s := NewMemoryStore()
	ten, err := s.CreateTenant(nil, "测", "ph")
	if err != nil {
		t.Fatal(err)
	}
	u, plain, err := s.CreateMember(nil, ten.TenantID, "abcdefgxyz", "pass1234", "测", "管理员", 0)
	if err != nil {
		t.Fatal(err)
	}
	if _, err := s.BindPhone(nil, u.UserID, "13900001111"); err != nil {
		t.Fatal(err)
	}
	got, err := s.Login(nil, "13900001111", plain)
	if err != nil || got.Username != "abcdefgxyz" {
		t.Fatalf("login by phone: %v %#v", err, got)
	}
	if _, err := s.Login(nil, "abcdefgxyz", plain); err != nil {
		t.Fatal(err)
	}
}
