func TestFetchAdminAccounts(t *testing.T) { db := modeltesthelper.NewMongoDB(t) defer db.Close() acc1 := createTestAccount(t) defer modelhelper.RemoveAccount(acc1.Id) acc2 := createTestAccount(t) defer modelhelper.RemoveAccount(acc2.Id) group, err := createGroup() if err != nil { t.Error(err) } accounts, err := modelhelper.FetchAdminAccounts(group.Slug) if err != nil { t.Error(err) } if len(accounts) != 0 { t.Errorf("accounts count should be 0, got: %d", len(accounts)) } if err := modelhelper.AddRelationship(&models.Relationship{ Id: bson.NewObjectId(), TargetId: acc1.Id, TargetName: "JAccount", SourceId: group.Id, SourceName: "JGroup", As: "admin", }); err != nil { t.Error(err) } if err := modelhelper.AddRelationship(&models.Relationship{ Id: bson.NewObjectId(), TargetId: acc2.Id, TargetName: "JAccount", SourceId: group.Id, SourceName: "JGroup", As: "admin", }); err != nil { t.Error(err) } accounts, err = modelhelper.FetchAdminAccounts(group.Slug) if err != nil { t.Error(err) } if len(accounts) != 2 { t.Errorf("accounts count should be 2, got: %d", len(accounts)) } }
func deleteGroups(res interface{}) error { g := res.(*models.Group) if isIn(g.Slug, "koding", "guests", "team") { return nil } admins, err := helper.FetchAdminAccounts(g.Slug) // if we have any admin, no need to delete if len(admins) > 0 { existingGroupBySlug.Set(g.Slug, struct{}{}) return nil } // if we have error other than not found, it is better not to delete if err != mgo.ErrNotFound { return nil } fmt.Printf("deleting jGroup %q\n", g.Slug) if !*flagDry { return helper.RemoveGroup(g.Id) } return nil }
func sendEventForCustomer(customerID string, eventName string, options map[string]interface{}) error { cus, err := customer.Get(customerID, nil) if err != nil { return err } if options == nil { options = make(map[string]interface{}) } for key, val := range cus.Meta { options[key] = val } admins, err := modelhelper.FetchAdminAccounts(cus.Meta["groupName"]) if err == mgo.ErrNotFound { return nil } if err != nil { return err } for _, admin := range admins { user, err := modelhelper.GetUser(admin.Profile.Nickname) if err != nil { return err } mail := &emailsender.Mail{ To: user.Email, Subject: eventName, Properties: &emailsender.Properties{ Username: user.Name, Options: options, }, } if err := mailSender(mail); err != nil { return err } } return nil }