func withTestServer(t *testing.T, f func(url string)) { const workerName = "pingtest" r := runner.New(workerName) if err := r.Init(); err != nil { t.Fatal(err) } c := config.MustRead(r.Conf.Path) // init mongo connection modelhelper.Initialize(c.Mongo) defer modelhelper.Close() port := tests.GetFreePort() mc := mux.NewConfig(workerName, "localhost", port) mc.Debug = r.Conf.Debug m := mux.New(mc, r.Log, r.Metrics) AddHandlers(m) m.Listen() go r.Listen() f(fmt.Sprintf("http://localhost:%s", port)) if err := r.Close(); err != nil { t.Fatalf("server close errored: %s", err.Error()) } // shutdown server m.Close() }
func main() { r := runner.New(Name) if err := r.Init(); err != nil { fmt.Println(err) return } appConfig := config.MustRead(r.Conf.Path) modelhelper.Initialize(appConfig.Mongo) defer modelhelper.Close() // create a realtime service provider instance. pubnub := models.NewPubNub(appConfig.GateKeeper.Pubnub, r.Log) defer pubnub.Close() mc := mux.NewConfig(Name, appConfig.GateKeeper.Host, appConfig.GateKeeper.Port) m := mux.New(mc, r.Log, r.Metrics) h := api.NewHandler(pubnub, appConfig, r.Log) h.AddHandlers(m) // consume messages from RMQ // Gatekeeper is not using RMQ, but runner is creating a message queue for // each worker. We need to discard the messages in the queue, otherwise // all the messages are piled up go r.Listen() m.Listen() defer m.Close() r.Wait() }
func main() { r := runner.New(Name) if err := r.Init(); err != nil { log.Fatal(err) } if r.Kite == nil { r.Log.Fatal("couldnt init kite") } // remove QOS, we want to consume all the messages from RMQ if err := r.Bongo.Broker.Sub.(*broker.Consumer).Consumer.QOS(0); err != nil { r.Log.Fatal("couldnt remove the QOS %# v", err) } appConfig := config.MustRead(r.Conf.Path) // init mongo connection modelhelper.Initialize(appConfig.Mongo) defer modelhelper.Close() // init with defaults & ensure expireAt index mongoCache := cache.NewMongoCacheWithTTL(modelhelper.Mongo.Session, cache.StartGC(), cache.MustEnsureIndexExpireAt()) defer mongoCache.StopGC() handler := collaboration.New(r.Log, mongoCache, appConfig, r.Kite) r.SetContext(handler) // only listen and operate on collaboration ping messages that are fired by the handler r.Register(models.Ping{}).On(collaboration.FireEventName).Handle((*collaboration.Controller).Ping) r.Listen() r.Wait() }
func main() { r := runner.New(Name) if err := r.Init(); err != nil { fmt.Println(err) return } // init mongo connection appConfig := config.MustRead(r.Conf.Path) modelhelper.Initialize(appConfig.Mongo) defer modelhelper.Close() r.SetContext(realtime.New(r.Bongo.Broker.MQ, r.Log)) r.Register(models.ChannelMessage{}).OnUpdate().Handle((*realtime.Controller).MessageUpdated) r.Register(models.MessageReply{}).OnCreate().Handle((*realtime.Controller).MessageReplySaved) r.Register(models.MessageReply{}).OnDelete().Handle((*realtime.Controller).MessageReplyDeleted) r.Register(models.ChannelMessageList{}).OnCreate().Handle((*realtime.Controller).MessageListSaved) r.Register(models.ChannelMessageList{}).OnUpdate().Handle((*realtime.Controller).ChannelMessageListUpdated) r.Register(models.ChannelMessageList{}).OnDelete().Handle((*realtime.Controller).MessageListDeleted) r.Register(models.ParticipantEvent{}).On(models.ChannelParticipant_Removed_From_Channel_Event).Handle((*realtime.Controller).ChannelParticipantRemoved) r.Register(models.ParticipantEvent{}).On(models.ChannelParticipant_Added_To_Channel_Event).Handle((*realtime.Controller).ChannelParticipantsAdded) r.Register(models.ChannelParticipant{}).OnUpdate().Handle((*realtime.Controller).ChannelParticipantUpdatedEvent) r.Register(models.Channel{}).OnDelete().Handle((*realtime.Controller).ChannelDeletedEvent) r.Register(models.Channel{}).OnUpdate().Handle((*realtime.Controller).ChannelUpdatedEvent) r.Listen() r.Wait() }
func main() { r := runner.New(Name) if err := r.Init(); err != nil { log.Fatal(err) } appConfig := config.MustRead(r.Conf.Path) modelhelper.Initialize(appConfig.Mongo) defer modelhelper.Close() segmentExporter := eventexporter.NewSegmentIOExporter(appConfig.Segment, QueueLength) datadogExporter := eventexporter.NewDatadogExporter(r.DogStatsD) // TODO // we are gonna add this line into the multiexporter // firstly, we need to make sure our json data satisfy druid's data specs // druidExporter := eventexporter.NewDruidExporter(appConfig.DruidHost) // exporter := eventexporter.NewMultiExporter(segmentExporter, datadogExporter, druidExporter) exporter := eventexporter.NewMultiExporter(segmentExporter, datadogExporter) constructor := emailsender.New(exporter, r.Log, appConfig) r.ShutdownHandler = constructor.Close r.SetContext(constructor) r.Register(emailsender.Mail{}).On(emailsender.SendEmailEventName).Handle((*emailsender.Controller).Process) r.Listen() r.Wait() }
func TestIndexSettingsDefaults(t *testing.T) { runner, handler := getTestHandler() defer runner.Close() appConfig := config.MustRead(runner.Conf.Path) modelhelper.Initialize(appConfig.Mongo) defer modelhelper.Close() Convey("given some handler", t, func() { err := handler.Init() So(err, ShouldBeNil) Convey("account email should not be retrievable", func() { indexSet, err := handler.indexes.Get(IndexAccounts) So(err, ShouldBeNil) settings, err := indexSet.Index.GetSettings() So(err, ShouldBeNil) found := false for _, item := range settings.UnretrievableAttributes { if item == "email" { found = true } } So(found, ShouldBeTrue) }) }) }
func (cmd *GroupFixDomain) Run(ctx context.Context) error { modelhelper.Initialize(envMongoURL()) defer modelhelper.Close() merr := new(multierror.Error) users := cmd.values for len(users) > 0 { var batch []string var records []*dnsclient.Record var ids []bson.ObjectId n := min(len(users), 100) batch, users = users[:n], users[n:] DefaultUi.Info(fmt.Sprintf("checking %d records...", n)) for _, user := range batch { rec, id, err := cmd.fixDomain(user) if err != nil { merr = multierror.Append(merr, err) continue } if cmd.dry { if rec == nil { merr = multierror.Append(merr, fmt.Errorf("domain for %q is ok", user)) } else { merr = multierror.Append(merr, fmt.Errorf("domain for %q is going to be upserted without -dry: %q", user, rec.Name)) } continue } if rec == nil { continue } records = append(records, rec) ids = append(ids, id) } if len(records) == 0 { continue } DefaultUi.Info(fmt.Sprintf("going to upsert %d domains", len(records))) // upsert domains if err := cmd.dns.UpsertRecords(records...); err != nil { merr = multierror.Append(merr, err) } // update domains if err := cmd.update(records, ids); err != nil { merr = multierror.Append(merr, err) } } return merr.ErrorOrNil() }
func (cmd *GroupDelete) Run(ctx context.Context) error { modelhelper.Initialize(envMongoURL()) defer modelhelper.Close() entries, err := cmd.listEntries(ctx, cmd.filter()) if err != nil { return err } items := make([]Item, len(entries)) for i, entry := range entries { items[i] = &Instance{ SoftlayerID: entry.ID, Domain: entry.Tags["koding-domain"], Username: entry.Tags["koding-user"], } } // TODO(rjeczalik): It's not possible to concurrently delete domains due to: // // ERROR could not delete domain "ukhscbd6fee9.kloudctl.dev.koding.io": // PriorRequestNotComplete: The request was rejected because Route 53 was // still processing a prior request.\n\tstatus code: 400, request id: // c8248760-b2e5-11e5-9b7d-33010efc6afe" // cmd.GroupThrottler.throttle = 1 return cmd.RunItems(ctx, items) }
func TestGetIdsFromMailboxHash(t *testing.T) { r := runner.New("test") if err := r.Init(); err != nil { t.Fatalf("couldnt start bongo %s", err.Error()) } defer r.Close() // init mongo connection appConfig := config.MustRead(r.Conf.Path) modelhelper.Initialize(appConfig.Mongo) defer modelhelper.Close() Convey("while getting ids from mailboxhash", t, func() { Convey("returns error if 1.index of mailboxhash doesn't exist", func() { m := &Mail{ MailboxHash: "message.", } gid, err := m.getIdsFromMailboxHash() So(err, ShouldNotBeNil) So(gid, ShouldEqual, 0) }) Convey("returns error if 1.index doesn't exist", func() { m := &Mail{ MailboxHash: "message.1234", } gid, err := m.getIdsFromMailboxHash() So(err, ShouldBeNil) So(gid, ShouldEqual, 1234) }) }) }
func TestValidate(t *testing.T) { r := runner.New("test") if err := r.Init(); err != nil { t.Fatalf("couldnt start bongo %s", err.Error()) } defer r.Close() // init mongo connection appConfig := config.MustRead(r.Conf.Path) modelhelper.Initialize(appConfig.Mongo) defer modelhelper.Close() Convey("while testing Validate", t, func() { Convey("From field of Mail struct should not be empty, otherwise return err", func() { m := &Mail{} err := m.Validate() So(err, ShouldNotBeNil) So(err, ShouldEqual, ErrFromFieldIsNotSet) }) Convey("TextBody field of Mail struct should not be empty, otherwise return err", func() { m := &Mail{From: "*****@*****.**"} err := m.Validate() So(err, ShouldNotBeNil) So(err, ShouldEqual, ErrTextBodyIsNotSet) }) Convey("returns nil if Mail struct is set ", func() { m := &Mail{From: "*****@*****.**", TextBody: "Some text parapraph"} So(m.Validate(), ShouldBeNil) }) }) }
func TestAccountParticipantAdded(t *testing.T) { runner, handler := getTestHandler() defer runner.Close() // init mongo connection appConfig := config.MustRead(runner.Conf.Path) modelhelper.Initialize(appConfig.Mongo) defer modelhelper.Close() Convey("given some account", t, func() { acc, err := models.CreateAccountInBothDbs() So(err, ShouldBeNil) So(acc, ShouldNotBeNil) tc1 := models.CreateTypedGroupedChannelWithTest( acc.Id, models.Channel_TYPE_TOPIC, models.Channel_KODING_NAME, ) models.AddParticipantsWithTest(tc1.Id, acc.Id) cp := &models.ChannelParticipant{ ChannelId: tc1.Id, AccountId: acc.Id, StatusConstant: models.ChannelParticipant_STATUS_ACTIVE, } Convey("when the account is already on algolia", func() { err := handler.AccountCreated(acc) So(err, ShouldBeNil) So(doBasicTestForAccount(handler, acc.OldId), ShouldBeNil) err = handler.ParticipantCreated(cp) So(err, ShouldBeNil) So(doBasicTestForAccount(handler, acc.OldId), ShouldBeNil) So(makeSureHasTag(handler, acc.OldId, strconv.FormatInt(tc1.Id, 10)), ShouldBeNil) Convey("trying to add again should success", func() { err = handler.ParticipantCreated(cp) So(err, ShouldBeNil) So(doBasicTestForAccount(handler, acc.OldId), ShouldBeNil) So(makeSureHasTag(handler, acc.OldId, strconv.FormatInt(tc1.Id, 10)), ShouldBeNil) So(makeSureTagLen(handler, acc.OldId, 2), ShouldBeNil) }) }) Convey("when the account is not on algolia", func() { err = handler.ParticipantCreated(cp) So(err, ShouldBeNil) So(doBasicTestForAccount(handler, acc.OldId), ShouldBeNil) So(makeSureHasTag(handler, acc.OldId, strconv.FormatInt(tc1.Id, 10)), ShouldBeNil) So(makeSureTagLen(handler, acc.OldId, 1), ShouldBeNil) }) }) }
func (cmd *GroupList) Run(ctx context.Context) error { modelhelper.Initialize(envMongoURL()) defer modelhelper.Close() if cmd.entries { return cmd.printEntries(ctx) } return cmd.printInstances(ctx) }
func TestCollaborationSesionEnd(t *testing.T) { r := runner.New("collaboration-tests") err := r.Init() if err != nil { panic(err) } defer r.Close() appConfig := config.MustRead(r.Conf.Path) modelhelper.Initialize(appConfig.Mongo) defer modelhelper.Close() Convey("while testing collaboration session end", t, func() { Convey("we should be able to create a doc on google drive", func() { Convey("we should be able to delete a created doc", func() { Convey("deleting an already deleted doc should not give error", func() { }) }) }) Convey("trying to delete a non-existing doc should not give error", func() { }) }) Convey("while pinging collaboration", t, func() { // owner owner, err := apimodels.CreateAccountInBothDbs() So(err, ShouldBeNil) So(owner, ShouldNotBeNil) groupName := apimodels.RandomGroupName() apimodels.CreateTypedGroupedChannelWithTest( owner.Id, apimodels.Channel_TYPE_GROUP, groupName, ) ownerSession, err := modelhelper.FetchOrCreateSession(owner.Nick, groupName) So(err, ShouldBeNil) So(ownerSession, ShouldNotBeNil) Convey("reponse should be success", func() { p := &models.Ping{ AccountId: 1, FileId: "hello", } res, err := rest.CollaborationPing(p, ownerSession.ClientId) So(err, ShouldBeNil) So(res, ShouldNotBeNil) }) }) }
func WithConfiguration(t *testing.T, f func(c *config.Config)) { r := runner.New("test") if err := r.Init(); err != nil { t.Fatal(err.Error()) } defer r.Close() c := config.MustRead(r.Conf.Path) modelhelper.Initialize(c.Mongo) defer modelhelper.Close() f(c) }
func WithRunner(t *testing.T, f func(*runner.Runner)) { r := runner.New("test") if err := r.Init(); err != nil { t.Fatalf("couldnt start bongo %s", err.Error()) } defer r.Close() appConfig := config.MustRead(r.Conf.Path) modelhelper.Initialize(appConfig.Mongo) defer modelhelper.Close() f(r) }
func (cmd *GroupStack) Run(ctx context.Context) error { modelhelper.Initialize(envMongoURL()) defer modelhelper.Close() var mu sync.Mutex var wg sync.WaitGroup merr := new(multierror.Error) jobs := make(chan string) go func() { for _, username := range cmd.groupValues.values { jobs <- username } close(jobs) }() DefaultUi.Info(fmt.Sprintf("processing %d users...", len(cmd.groupValues.values))) for range make([]struct{}, cmd.jobs) { wg.Add(1) go func() { defer wg.Done() for username := range jobs { sd, err := cmd.details(username) if err != nil { merr = multierror.Append(merr, err) continue } if cmd.rm { err = modelhelper.RemoveFromStack(sd) } else { err = modelhelper.AddToStack(sd) } if err != nil { DefaultUi.Warn(fmt.Sprintf("processing %q user stack failed: %s", username, err)) mu.Lock() merr = multierror.Append(merr, err) mu.Unlock() } else { DefaultUi.Warn(fmt.Sprintf("processed %q user stack", username)) } } }() } wg.Wait() return merr.ErrorOrNil() }
func (cmd *GroupCreate) Run(ctx context.Context) (err error) { modelhelper.Initialize(envMongoURL()) defer modelhelper.Close() var spec *MachineSpec if !cmd.dry { spec, err = ParseMachineSpec(cmd.file) } else { spec = &MachineSpec{ Machine: models.Machine{ Provider: "softlayer", Users: []models.MachineUser{{ Username: "******", }}, Slug: "softlayer-vm-0", }, } } if err != nil { return err } var specs []*MachineSpec if len(cmd.values) != 0 { specs, err = cmd.multipleUserSpecs(spec) } else { specs, err = cmd.multipleMachineSpecs(spec) } if len(specs) == 0 { if err != nil { DefaultUi.Warn(err.Error()) } return errors.New("nothing to build") } if err != nil { DefaultUi.Warn(err.Error()) } items := make([]Item, len(specs)) for i, spec := range specs { items[i] = spec } if e := cmd.RunItems(ctx, items); e != nil { return multierror.Append(err, e).ErrorOrNil() } return err }
func main() { r := runner.New(name) if err := r.Init(); err != nil { log.Fatal(err.Error()) } appConfig := config.MustRead(r.Conf.Path) modelhelper.Initialize(appConfig.Mongo) defer modelhelper.Close() r.SetContext(presence.New(r.Log, appConfig)) r.Register(presence.Ping{}).On(presence.EventName).Handle((*presence.Controller).Ping) r.Listen() r.Wait() }
func main() { r := runner.New(Name) if err := r.Init(); err != nil { log.Fatal(err) } // init mongo connection appConfig := config.MustRead(r.Conf.Path) modelhelper.Initialize(appConfig.Mongo) defer modelhelper.Close() if err := models.DeleteDiffedDBAccounts(); err != nil { fmt.Println("error while deleting account that non-existing in mongo", err) return } }
func main() { r := runner.New(Name) if err := r.Init(); err != nil { fmt.Println(err) return } defer r.Close() // init mongo connection appConfig := config.MustRead(r.Conf.Path) modelhelper.Initialize(appConfig.Mongo) defer modelhelper.Close() algolia := algoliasearch.NewClient( appConfig.Algolia.AppId, appConfig.Algolia.ApiSecretKey, ) // create message handler handler := algoliaconnector.New(r.Log, algolia, appConfig.Algolia.IndexSuffix) counter := 0 for b := 0; ; b++ { var accounts []models.Account err := (&models.Account{}).Some(&accounts, &bongo.Query{ Pagination: *bongo.NewPagination(100, b*100), }) if err != nil { r.Log.Error(err.Error()) continue } for _, account := range accounts { counter++ r.Log.Info("[%d]: currently migrating: '%v'", counter, account.Nick) if err := handler.AccountUpdated(&account); err != nil { r.Log.Error(err.Error()) continue } } if len(accounts) < 100 { break } } }
func TestGetSocialIdFromEmail(t *testing.T) { r := runner.New("test") if err := r.Init(); err != nil { t.Fatalf("couldnt start bongo %s", err.Error()) } defer r.Close() // init mongo connection appConfig := config.MustRead(r.Conf.Path) modelhelper.Initialize(appConfig.Mongo) defer modelhelper.Close() Convey("while getting account id in the mail", t, func() { Convey("From fields should be saved in db, otherwise return err", func() { m := &Mail{ From: "mailisnotexist@abo", } gid, err := m.getSocialIdFromEmail() So(err, ShouldNotBeNil) So(err, ShouldEqual, ErrEmailIsNotFetched) So(gid, ShouldEqual, 0) }) Convey("should not be any error if all is well", func() { acc, err := socialapimodels.CreateAccountInBothDbs() So(err, ShouldBeNil) mongoUser, err := modelhelper.GetUser(acc.Nick) So(err, ShouldBeNil) m := &Mail{ From: mongoUser.Email, } gid, err := m.getSocialIdFromEmail() So(err, ShouldBeNil) So(gid, ShouldEqual, acc.Id) }) }) }
func TestCollaborationOperationsDeleteDriveDoc(t *testing.T) { r := runner.New("collaboration-DeleteDriveDoc-tests") err := r.Init() if err != nil { panic(err) } defer r.Close() appConfig := config.MustRead(r.Conf.Path) modelhelper.Initialize(appConfig.Mongo) defer modelhelper.Close() // init with defaults mongoCache := cache.NewMongoCacheWithTTL(modelhelper.Mongo.Session) defer mongoCache.StopGC() handler := New(r.Log, mongoCache, appConfig, r.Kite) Convey("while testing DeleteDriveDoc", t, func() { req := &models.Ping{ AccountId: 1, FileId: fmt.Sprintf("%d", rand.Int63()), } Convey("should be able to create the file", func() { f, err := createTestFile(handler) So(err, ShouldBeNil) req.FileId = f.Id Convey("should be able to delete the created file", func() { err = handler.DeleteDriveDoc(req) So(err, ShouldBeNil) }) Convey("if file id is nil response should be nil", func() { req := req req.FileId = "" err = handler.DeleteDriveDoc(req) So(err, ShouldBeNil) }) }) }) }
func TestMailParse(t *testing.T) { r := runner.New("test") err := r.Init() if err != nil { panic(err) } defer r.Close() appConfig := config.MustRead(r.Conf.Path) modelhelper.Initialize(appConfig.Mongo) defer modelhelper.Close() Convey("while sending mail", t, func() { Convey("reponse should be success", func() { acc, err := socialapimodels.CreateAccountInBothDbs() So(err, ShouldBeNil) c := socialapimodels.CreateChannelWithTest(acc.Id) socialapimodels.AddParticipantsWithTest(c.Id, acc.Id) cm := socialapimodels.CreateMessage(c.Id, acc.Id, socialapimodels.ChannelMessage_TYPE_POST) So(cm, ShouldNotBeNil) mongoUser, err := modelhelper.GetUser(acc.Nick) So(err, ShouldBeNil) p := &models.Mail{ From: mongoUser.Email, OriginalRecipient: fmt.Sprintf("*****@*****.**", c.Id), MailboxHash: fmt.Sprintf("messageid.%d", cm.Id), TextBody: "Its an example of text message", StrippedTextReply: "This one is reply message", } res, err := rest.MailParse(p) So(err, ShouldBeNil) So(res, ShouldNotBeNil) }) }) }
func TestGetAccount(t *testing.T) { r := runner.New("test") if err := r.Init(); err != nil { t.Fatalf("couldnt start bongo %s", err.Error()) } defer r.Close() // init mongo connection appConfig := config.MustRead(r.Conf.Path) modelhelper.Initialize(appConfig.Mongo) defer modelhelper.Close() Convey("while testing get account", t, func() { Convey("returns empty if parameter is invalid", func() { acc, err := GetAccount("interestingEmail@somethinglikethat") So(err, ShouldNotBeNil) So(acc, ShouldBeNil) }) Convey("returns empty if parameter is empty", func() { acc, err := GetAccount("") So(err, ShouldNotBeNil) So(acc, ShouldBeNil) }) Convey("Should return blank if parameter is empty", func() { acc, err := socialapimodels.CreateAccountInBothDbs() So(err, ShouldBeNil) mongoUser, err := modelhelper.GetUser(acc.Nick) So(err, ShouldBeNil) m := &Mail{ From: mongoUser.Email, } ga, err := GetAccount(m.From) So(err, ShouldBeNil) So(ga, ShouldNotBeNil) }) }) }
func main() { r := runner.New(Name) if err := r.Init(); err != nil { log.Fatal(err) } appConfig := config.MustRead(r.Conf.Path) algolia := algoliasearch.NewClient(appConfig.Algolia.AppId, appConfig.Algolia.ApiSecretKey) modelhelper.Initialize(appConfig.Mongo) defer modelhelper.Close() // create message handler handler := algoliaconnector.New(r.Log, algolia, appConfig.Algolia.IndexSuffix) index, ok := indexes.(map[string]interface{}) items, ok := index["items"].([]interface{}) if !ok { return } for _, item := range items { it, ok := item.(map[string]interface{}) if !ok { continue } updatedAt := it["updatedAt"].(string) t, err := time.Parse(time.RFC3339, updatedAt) if err != nil { fmt.Println("parsing time:", err) return } if t.Before(GetLast60Days()) { if _, err := handler.InitAndDeleteIndex(it["name"].(string)); err != nil { fmt.Println(err) return } } } r.Wait() }
func main() { r := runner.New(Name) if err := r.Init(); err != nil { log.Fatal(err) } // init mongo connection appConfig := config.MustRead(r.Conf.Path) modelhelper.Initialize(appConfig.Mongo) defer modelhelper.Close() r.SetContext(team.NewController(r.Log, appConfig)) r.Register(models.ChannelParticipant{}).OnCreate().Handle((*team.Controller).HandleParticipant) r.Register(models.ChannelParticipant{}).OnUpdate().Handle((*team.Controller).HandleParticipant) r.Register(models.ChannelParticipant{}).OnDelete().Handle((*team.Controller).HandleParticipant) r.Register(models.Channel{}).OnCreate().Handle((*team.Controller).HandleCreator) r.Register(models.Channel{}).OnDelete().Handle((*team.Controller).HandleChannel) r.Listen() r.Wait() }
func main() { r := runner.New(Name) if err := r.Init(); err != nil { log.Fatal(err) } appConfig := config.MustRead(r.Conf.Path) algolia := algoliasearch.NewClient(appConfig.Algolia.AppId, appConfig.Algolia.ApiSecretKey) modelhelper.Initialize(appConfig.Mongo) defer modelhelper.Close() // create message handler handler := algoliaconnector.New(r.Log, algolia, appConfig.Algolia.IndexSuffix) if err := handler.DeleteNicksWithQueryBrowseAll(""); err != nil { r.Log.Error("Could not remove guest accounts: %s", err) } r.Wait() }
func main() { r := runner.New(Name) if err := r.Init(); err != nil { log.Fatal(err.Error()) } appConfig := config.MustRead(r.Conf.Path) // init mongo connection modelhelper.Initialize(appConfig.Mongo) defer modelhelper.Close() algolia := algoliasearch.NewClient(appConfig.Algolia.AppId, appConfig.Algolia.ApiSecretKey) // create message handler handler := algoliaconnector.New(r.Log, algolia, appConfig.Algolia.IndexSuffix) if err := handler.Init(); err != nil { // this is not a blocker for algoliaconnector worker, we can continue working r.Log.Error("Err while init: %s", err.Error()) } r.SetContext(handler) r.Register(models.Channel{}).OnCreate().Handle((*algoliaconnector.Controller).ChannelCreated) r.Register(models.Channel{}).OnUpdate().Handle((*algoliaconnector.Controller).ChannelUpdated) r.Register(models.Account{}).OnCreate().Handle((*algoliaconnector.Controller).AccountCreated) r.Register(models.Account{}).OnUpdate().Handle((*algoliaconnector.Controller).AccountUpdated) r.Register(models.ChannelMessageList{}).OnCreate().Handle((*algoliaconnector.Controller).MessageListSaved) r.Register(models.ChannelMessageList{}).OnDelete().Handle((*algoliaconnector.Controller).MessageListDeleted) r.Register(models.ChannelMessage{}).OnUpdate().Handle((*algoliaconnector.Controller).MessageUpdated) // participant related events r.Register(models.ChannelParticipant{}).OnCreate().Handle((*algoliaconnector.Controller).ParticipantCreated) r.Register(models.ChannelParticipant{}).OnUpdate().Handle((*algoliaconnector.Controller).ParticipantUpdated) r.Listen() r.Wait() }
func TestCollaboration(t *testing.T) { r := runner.New("collaboration-tests") err := r.Init() if err != nil { panic(err) } defer r.Close() appConfig := config.MustRead(r.Conf.Path) modelhelper.Initialize(appConfig.Mongo) defer modelhelper.Close() // init with defaults mongoCache := cache.NewMongoCacheWithTTL(modelhelper.Mongo.Session) defer mongoCache.StopGC() handler := New(r.Log, mongoCache, appConfig, r.Kite) Convey("while pinging collaboration", t, func() { // owner owner := apimodels.NewAccount() owner.OldId = AccountOldId.Hex() owner, err := rest.CreateAccount(owner) So(err, ShouldBeNil) So(owner, ShouldNotBeNil) groupName := apimodels.RandomGroupName() ownerSession, err := modelhelper.FetchOrCreateSession(owner.Nick, groupName) So(err, ShouldBeNil) So(ownerSession, ShouldNotBeNil) rand.Seed(time.Now().UnixNano()) req := &models.Ping{ AccountId: 1, FileId: fmt.Sprintf("%d", rand.Int63()), } Convey("while testing Ping", func() { Convey("reponse should be success with valid ping", func() { err = handler.Ping(req) So(err, ShouldBeNil) }) Convey("reponse should be success with invalid FileId", func() { req.FileId = "" err = handler.Ping(req) So(err, ShouldBeNil) }) Convey("reponse should be success with invalid AccountId", func() { req.AccountId = 0 err = handler.Ping(req) So(err, ShouldBeNil) }) Convey("reponse should be success with invalid session", func() { req := req // prepare an invalid session here req.CreatedAt = time.Now().UTC() err = mongoCache.SetEx(PrepareFileKey(req.FileId), ExpireSessionKeyDuration, req.CreatedAt.Add(-terminateSessionDuration)) err = handler.Ping(req) So(err, ShouldBeNil) }) Convey("after sleep time", func() { req := req Convey("expired session should get invalidSessoin", func() { st := sleepTime sleepTime = time.Millisecond * 110 tsd := terminateSessionDuration terminateSessionDuration = 100 // set durations back to the original value defer func() { sleepTime = st terminateSessionDuration = tsd }() req.CreatedAt = time.Now().UTC() // prepare a valid key err = mongoCache.SetEx( PrepareFileKey(req.FileId), terminateSessionDuration, // expire the key after this period req.CreatedAt.Unix()) // while sleeping here, redis key should be removed // and we can understand that the Collab session is expired time.Sleep(sleepTime) req := req err = handler.wait(req) So(err, ShouldEqual, errSessionInvalid) }) Convey("deadlined session should get errDeadlineReached", func() { st := sleepTime sleepTime = time.Millisecond * 110 dd := deadLineDuration deadLineDuration = 100 // set durations back to the original value defer func() { sleepTime = st deadLineDuration = dd }() req := req err := handler.wait(req) So(err, ShouldEqual, errDeadlineReached) }) }) }) Convey("while testing checkIfKeyIsValid", func() { req := req req.CreatedAt = time.Now().UTC() // prepare a valid key err := mongoCache.SetEx( PrepareFileKey(req.FileId), ExpireSessionKeyDuration, // expire the key after this period req.CreatedAt.Unix(), // value - unix time ) So(err, ShouldBeNil) Convey("valid key should return nil", func() { err = handler.checkIfKeyIsValid(req) So(err, ShouldBeNil) }) Convey("invalid key should return errSessionInvalid", func() { req := req // override fileId req.FileId = fmt.Sprintf("%d", rand.Int63()) err = handler.checkIfKeyIsValid(req) So(err, ShouldEqual, errSessionInvalid) }) Convey("invalid (non-timestamp) value should return errSessionInvalid", func() { req := req req.CreatedAt = time.Now().UTC() err = mongoCache.SetEx( PrepareFileKey(req.FileId), ExpireSessionKeyDuration, // expire the key after this period "req.CreatedAt.Unix()", // replace timestamp with unix time ) err = handler.checkIfKeyIsValid(req) So(err, ShouldEqual, errSessionInvalid) }) Convey("old ping time should return errSessionInvalid", func() { req := req req.CreatedAt = time.Now().UTC() err = mongoCache.SetEx( PrepareFileKey(req.FileId), ExpireSessionKeyDuration, // expire the key after this period req.CreatedAt.Add(-terminateSessionDuration).Unix(), ) err = handler.checkIfKeyIsValid(req) So(err, ShouldEqual, errSessionInvalid) }) Convey("previous ping time is in safe area", func() { req := req testPingTimes(req, -1, mongoCache, handler, nil) }) Convey("0 ping time is in safe area", func() { req := req testPingTimes(req, 0, mongoCache, handler, nil) }) Convey("2 ping time is in safe area", func() { req := req testPingTimes(req, 2, mongoCache, handler, nil) }) Convey("3 ping time is in safe area", func() { req := req testPingTimes(req, 3, mongoCache, handler, nil) }) Convey("4 ping time is not in safe area - because we already reverted the time ", func() { req := req testPingTimes(req, 4, mongoCache, handler, errSessionInvalid) }) Convey("5 ping time is not in safe area ", func() { req := req testPingTimes(req, 5, mongoCache, handler, errSessionInvalid) }) }) }) }
func (m *Migrate) Action(args []string) error { if err := m.Valid(); err != nil { return err } c, err := kloudClient() if err != nil { return err } defer c.Close() modelhelper.Initialize(*m.MongoURL) defer modelhelper.Close() account, err := modelhelper.GetAccount(*m.User) if err != nil { return err } if *m.Credential == "" { *m.Credential = bson.NewObjectId().Hex() cred := &models.Credential{ Id: bson.NewObjectId(), Provider: "aws", Identifier: *m.Credential, OriginId: account.Id, } credData := &models.CredentialData{ Id: bson.NewObjectId(), Identifier: *m.Credential, OriginId: account.Id, Meta: bson.M{ "access_key": *m.Key, "secret_key": *m.Secret, "region": *m.Region, }, } if err := modelhelper.InsertCredential(cred, credData); err != nil { return err } credRelationship := &models.Relationship{ Id: bson.NewObjectId(), TargetId: cred.Id, TargetName: "JCredential", SourceId: account.Id, SourceName: "JAccount", As: "owner", } if err := modelhelper.AddRelationship(credRelationship); err != nil { return err } } req := &MigrateRequest{ Provider: "aws", Machines: m.Machines, Identifier: *m.Credential, GroupName: *m.Team, StackName: "Migrated Stack", Impersonate: *m.User, } _, err = c.Tell("migrate", req) return err }