func TestDomainsByOwner(t *testing.T) { c := dmgr_setup() defer dmgr_teardown(c, t) mgr := DomainManager(&MongoDomainManager{c}) uid1, uid2 := gouuid.New(), gouuid.New() mgr.ClaimDomain("gotest_user1_1.org", &uid1) mgr.ClaimDomain("gotest_user1_2.org", &uid1) mgr.ClaimDomain("gotest_user2_1.org", &uid2) mgr.ClaimDomain("gotest_user2_2.org", &uid2) got, err := mgr.DomainsByOwner(&uid1) if err != nil { t.Fatalf("Could find domains: %s", err) } expected := []*Domain{ &Domain{ Name: "gotest_user1_1.org", Owners: []*gouuid.UUID{&uid1}, Aliases: []*Alias{}, }, &Domain{ Name: "gotest_user1_2.org", Owners: []*gouuid.UUID{&uid1}, Aliases: []*Alias{}, }, } if !reflect.DeepEqual(got, expected) { t.Fatalf("Unexpected domain data. Got %#v, expected %#v", got, expected) } }
func (mum *MongoUserManager) New(authenticator, id string) (*User, error) { uid, apikey := gouuid.New(), gouuid.New() user := &User{ UID: &uid, APIKey: &apikey, Authenticators: map[string]string{ authenticator: id, }, } return user, mum.UpdateUser(user) }
func TestClaim(t *testing.T) { c := dmgr_setup() defer dmgr_teardown(c, t) mgr := DomainManager(&MongoDomainManager{c}) uid := gouuid.New() err := mgr.ClaimDomain("gotest.org", &uid) if err != nil { t.Fatalf("ClaimDomain failed: %s", err) } qry := c.Find(bson.M{}) if count, _ := qry.Count(); count != 1 { t.Fatalf("Unexpected number of results: %d", count) } got, expected := Domain{}, Domain{ Name: "gotest.org", Owners: []*gouuid.UUID{&uid}, Aliases: []*Alias{}, } err = qry.One(&got) if err != nil { t.Fatalf("Could not get domain: %s", err) } if !reflect.DeepEqual(got, expected) { t.Fatalf("Unexpected domain data. Got %#v, expected %#v", got, expected) } }
func TestFindAlias(t *testing.T) { c := dmgr_setup() defer dmgr_teardown(c, t) mgr := DomainManager(&MongoDomainManager{c}) uid := gouuid.New() mgr.ClaimDomain("gotest.org", &uid) expected := []*Alias{ &Alias{ RepoURL: "repo1", RepoType: "git", ForwardURL: "homepage1", Alias: "alias1", }, &Alias{ RepoURL: "repo2", RepoType: "git", ForwardURL: "homepage2", Alias: "alias2", }, } mgr.SetAlias("gotest.org", expected[0], &uid) mgr.SetAlias("gotest.org", expected[1], &uid) alias, err := mgr.FindAlias("gotest.org", "alias1") if err != nil { t.Fatalf("Could not find domain: %s", err) } if !reflect.DeepEqual(alias, expected[0]) { t.Fatalf("Unexpected domain data. Got %#v, expected %#v", alias, expected) } }
func TestDeleteAlias(t *testing.T) { c := dmgr_setup() defer dmgr_teardown(c, t) mgr := DomainManager(&MongoDomainManager{c}) uid := gouuid.New() mgr.ClaimDomain("gotest.org", &uid) expected := []*Alias{ &Alias{ RepoURL: "repo1", RepoType: "git", ForwardURL: "homepage1", Alias: "alias1", }, &Alias{ RepoURL: "repo2", RepoType: "git", ForwardURL: "homepage2", Alias: "alias2", }, } mgr.SetAlias("gotest.org", expected[0], &uid) mgr.SetAlias("gotest.org", expected[1], &uid) mgr.DeleteAlias(expected[1].ID, &uid) expected = expected[0:1] domain := Domain{} c.Find(bson.M{}).One(&domain) if !reflect.DeepEqual(domain.Aliases, expected) { t.Fatalf("Unexpected domain data. Got %#v, expected %#v", domain.Aliases, expected) } }
func TestDeleteDomain(t *testing.T) { c := dmgr_setup() defer dmgr_teardown(c, t) mgr := DomainManager(&MongoDomainManager{c}) uid := gouuid.New() mgr.ClaimDomain("gotest.org", &uid) mgr.ClaimDomain("gotest2.org", &uid) err := mgr.DeleteDomain("gotest.org", &uid) if err != nil { t.Fatalf("Could not delete domain: %s", err) } got, err := mgr.DomainsByOwner(&uid) if err != nil { t.Fatalf("Could not get remaining domains: %s", err) } expected := []*Domain{ &Domain{ Name: "gotest2.org", Owners: []*gouuid.UUID{&uid}, Aliases: []*Alias{}, }, } if !reflect.DeepEqual(got, expected) { t.Fatalf("Unexpected domain data. Got %#v, expected %#v", got, expected) } }
func init() { // Register *gouuid.UUID as a type with gob // as gob is being used bei gorilla’s cookie session store which // is used by us to save the user’s uuid. uuid := gouuid.New() gob.Register(&uuid) }
func TestFindUser(t *testing.T) { c := umgr_setup() defer umgr_teardown(c, t) mgr := UserManager(&MongoUserManager{c}) uuid := gouuid.New() err := c.Insert( &User{ APIKey: &uuid, Authenticators: map[string]string{ "gotest": TOKEN, }, }) if err != nil { panic(err) } user, err := mgr.FindByAuthenticator("gotest", TOKEN) if err != nil { t.Fatalf("Could not get user: %s", err) } if !reflect.DeepEqual(user.APIKey, &uuid) { t.Fatalf("Unexpected APIKey for user") } user2, err := mgr.FindByAPIKey(&uuid) if err != nil { t.Fatalf("Could not get user: %s", err) } if !reflect.DeepEqual(user, user2) { t.Fatalf("Unexpected user data") } }
func TestDeleteUnknownDomain(t *testing.T) { c := dmgr_setup() defer dmgr_teardown(c, t) mgr := DomainManager(&MongoDomainManager{c}) uid := gouuid.New() other_uid := gouuid.New() mgr.ClaimDomain("gotest.org", &uid) mgr.ClaimDomain("gotest2.org", &uid) err := mgr.DeleteDomain("gotest.org", &other_uid) if err == nil { t.Fatalf("Could delete domain: %s", err) } got, err := mgr.DomainsByOwner(&uid) if err != nil { t.Fatalf("Could not get remaining domains: %s", err) } if len(got) != 2 { t.Fatalf("Got unexpected number of remaining domains") } }
func TestFindDomain(t *testing.T) { c := dmgr_setup() defer dmgr_teardown(c, t) mgr := DomainManager(&MongoDomainManager{c}) uid, other_uid := gouuid.New(), gouuid.New() mgr.ClaimDomain("gotest.org", &uid) mgr.ClaimDomain("gotest2.org", &other_uid) got, err := mgr.FindDomain("gotest.org") if err != nil { t.Fatalf("Could find domain: %s", err) } expected := &Domain{ Name: "gotest.org", Owners: []*gouuid.UUID{&uid}, Aliases: []*Alias{}, } if !reflect.DeepEqual(got, expected) { t.Fatalf("Unexpected domain data. Got %#v, expected %#v", got, expected) } }
func (mdm *MongoDomainManager) SetAlias(name string, alias *Alias, uid *gouuid.UUID) error { // Update if alias.ID != nil { mdm.Collection.EnsureIndexKey("aliases.id") return mdm.Collection.Update(bson.M{ "aliases.id": alias.ID, }, bson.M{ "$set": bson.M{ "aliases.$": alias, }, }) } aid := gouuid.New() alias.ID = &aid mdm.Collection.EnsureIndexKey("name", "owners") return mdm.Collection.Update(bson.M{ "name": name, "owners": uid, }, bson.M{ "$push": bson.M{ "aliases": alias, }, }) }
func generateID() string { return gouuid.New().ShortString() }
func TestNumber(t *testing.T) { _ = gouuid.New() if Number() != 4 { t.Fatalf("AAAH!") } }