package oltp
import (
"strings"
"testing"
)
// A fossil login linked to a forge account wears that account's picture
// on its people page and beside its name; a login linked to two
// different accounts in two repositories stays on its identicon; a
// login's own picture wins.
func TestALinkedLoginWearsItsAccountsPicture(t *testing.T) {
d := openTest(t)
set := func(name, etag string) {
if err := d.SetAvatar(Avatar{Kind: AvatarUser, Name: name, Key: "k" + name, Mime: "image/png", ETag: etag, Bytes: 1}); err != nil {
t.Fatal(err)
}
}
for _, acct := range []string{"dmestas", "eve"} {
if err := d.AddAccount(acct, "pw12345678", false); err != nil {
t.Fatal(err)
}
}
set("dmestas", strings.Repeat("a", 64))
set("eve", strings.Repeat("e", 64))
for _, repo := range []string{"sqlite", "tcl"} {
if _, _, err := d.ReplaceFossilUsers(repo, []FossilUser{{Login: "dan"}, {Login: "drh"}}); err != nil {
t.Fatal(err)
}
}
link := func(repo, login, acct string) {
if err := linkErr(d, repo, login, acct); err != nil {
t.Fatal(err)
}
}
link("sqlite", "dan", "dmestas")
link("sqlite", "drh", "dmestas")
link("tcl", "drh", "eve") // drh is two different people here
if a, ok, _ := d.AvatarOf(AvatarUser, "dan"); !ok || a.ETag[:4] != "aaaa" {
t.Fatalf("FAIL: the linked login dan does not wear dmestas's picture (%v)", ok)
}
tags, err := d.AvatarETags(AvatarUser)
if err != nil {
t.Fatal(err)
}
if tags["dan"][:4] != "aaaa" {
t.Fatalf("FAIL: the listing does not carry dan's picture: %v", tags)
}
if _, ok := tags["drh"]; ok {
t.Fatal("FAIL: drh, linked to two different accounts, took one of their pictures")
}
if _, ok, _ := d.AvatarOf(AvatarUser, "drh"); ok {
t.Fatal("FAIL: drh is served a picture though the link is ambiguous")
}
set("dan", strings.Repeat("d", 64))
if a, _, _ := d.AvatarOf(AvatarUser, "dan"); a.ETag[:4] != "dddd" {
t.Fatal("FAIL: dan's own picture does not win over the linked account's")
}
t.Log("ok: linked login wears the account's picture; ambiguous link stays unpictured; own picture wins")
}