package oltp
import "testing"
// A bridged name is linked to an account only by someone who stands
// behind it, only to an account that exists, and the link goes with the
// account and the repository.
func TestChatIdentityLinksABridgedNameToAnAccount(t *testing.T) {
d := testDB(t)
if err := d.AddRepo("libfossil", t.TempDir(), "", Public); err != nil {
t.Fatal(err)
}
for _, n := range []string{"dmestas", "stephan"} {
if err := d.AddAccount(n, "a-long-enough-password", false); err != nil {
t.Fatal(err)
}
}
if err := d.SetChatIdentity("libfossil", "stephan", "stephan", "dmestas"); err == nil {
t.Fatal("FAIL: a name that is not fossil:<login> was linked")
}
if err := d.SetChatIdentity("libfossil", "fossil:drh", "drh", "dmestas"); err == nil {
t.Fatal("FAIL: a link to an account that does not exist was stored")
}
if err := d.SetChatIdentity("libfossil", "fossil:dmestas", "dmestas", "bridge"); err != nil {
t.Fatal(err)
}
if err := d.SetChatIdentity("libfossil", "fossil:stephan", "stephan", "dmestas"); err != nil {
t.Fatal(err)
}
got, err := d.ChatIdentities("libfossil")
if err != nil || len(got) != 2 || got[1].Remote != "fossil:stephan" || got[1].Account != "stephan" || got[1].By != "dmestas" {
t.Fatalf("FAIL: links = %+v (%v)", got, err)
}
t.Log("ok: links are stored with who made them, and refused for a non-name or a missing account")
if err := d.SetChatIdentity("libfossil", "fossil:stephan", "", "dmestas"); err != nil {
t.Fatal(err)
}
if got, _ := d.ChatIdentities("libfossil"); len(got) != 1 {
t.Fatalf("FAIL: removing left %+v", got)
}
if err := d.DeleteAccount("dmestas"); err != nil {
t.Fatal(err)
}
if got, _ := d.ChatIdentities("libfossil"); len(got) != 0 {
t.Fatalf("FAIL: a deleted account is still linked: %+v", got)
}
t.Log("ok: a link is removed by an empty account and goes with its account")
}