})

	It("deletes a template", func() {
		collection.GetCall.Returns.Template = collections.Template{
			ID: "some-template-id",
		}
		handler.ServeHTTP(writer, request, context)

		Expect(writer.Code).To(Equal(http.StatusNoContent))
		Expect(writer.Body.String()).To(BeEmpty())

		Expect(collection.GetCall.Receives.Connection).To(Equal(conn))
		Expect(collection.GetCall.Receives.TemplateID).To(Equal("some-template-id"))
		Expect(collection.GetCall.Receives.ClientID).To(Equal("some-client-id"))

		Expect(collection.DeleteCall.Receives.Connection).To(Equal(database.Connection()))
		Expect(collection.DeleteCall.Receives.TemplateID).To(Equal("some-template-id"))
	})

	Context("failure cases", func() {
		It("returns a 404 if the template does not exist when getting", func() {
			collection.GetCall.Returns.Error = collections.NotFoundError{errors.New(`Template with id "some-template-id" could not be found`)}

			handler.ServeHTTP(writer, request, context)
			Expect(writer.Code).To(Equal(http.StatusNotFound))
			Expect(writer.Body.String()).To(MatchJSON(`{
				"errors": ["Template with id \"some-template-id\" could not be found"]
			}`))
		})

		It("returns a 404 if the template does not exist when deleting", func() {
Esempio n. 2
0
		orgStrategy   *mocks.Strategy
		emailStrategy *mocks.Strategy
		database      *mocks.Database
	)
	BeforeEach(func() {
		userStrategy = mocks.NewStrategy()
		spaceStrategy = mocks.NewStrategy()
		orgStrategy = mocks.NewStrategy()
		emailStrategy = mocks.NewStrategy()
		database = mocks.NewDatabase()
		determiner = strategy.NewStrategyDeterminer(userStrategy, spaceStrategy, orgStrategy, emailStrategy)
	})

	Context("when dispatching to a user", func() {
		It("determines the strategy and calls it", func() {
			err := determiner.Determine(database.Connection(), "some-uaa-host", gobble.NewJob(queue.CampaignJob{
				Campaign: collections.Campaign{
					ID:             "some-id",
					SendTo:         map[string]string{"user": "******"},
					CampaignTypeID: "some-campaign-type-id",
					Text:           "some-text",
					HTML:           "<h1>my-html</h1>",
					Subject:        "The Best subject",
					TemplateID:     "some-template-id",
					ReplyTo:        "*****@*****.**",
					ClientID:       "some-client-id",
				},
			}))

			Expect(err).NotTo(HaveOccurred())
			Expect(userStrategy.DispatchCall.Receives.Dispatch).To(Equal(services.Dispatch{
	var (
		campaignStatusUpdater postal.CampaignStatusUpdater
		messagesRepo          *mocks.MessagesRepository
		campaignsRepo         *mocks.CampaignsRepository
		pollInterval          time.Duration
		database              *mocks.Database
	)

	BeforeEach(func() {
		database = mocks.NewDatabase()
		database.ConnectionCall.Returns.Connection = mocks.NewConnection()
		messagesRepo = mocks.NewMessagesRepository()
		campaignsRepo = mocks.NewCampaignsRepository()
		pollInterval = 1 * time.Millisecond

		campaignStatusUpdater = postal.NewCampaignStatusUpdater(database.Connection(),
			messagesRepo,
			campaignsRepo,
			pollInterval)
	})

	Describe("Run", func() {
		It("starts the worker to gather campaign ids", func() {
			campaignStatusUpdater.Run()

			Eventually(func() int {
				return len(campaignsRepo.ListSendingCampaignsCall.Invocations)
			}).Should(BeNumerically(">=", 2))

			call1 := campaignsRepo.ListSendingCampaignsCall.Invocations[0]
			call2 := campaignsRepo.ListSendingCampaignsCall.Invocations[1]
			"template_id":      "random-template-id",
			"reply_to":         "reply-to-address",
		})
		Expect(err).NotTo(HaveOccurred())

		request, err = http.NewRequest("POST", "/senders/some-sender-id/campaigns", bytes.NewBuffer(requestBody))
		Expect(err).NotTo(HaveOccurred())

		handler.ServeHTTP(writer, request, context)

		Expect(writer.Code).To(Equal(http.StatusAccepted))
		Expect(writer.Body.String()).To(MatchJSON(`{
			"campaign_id": "my-campaign-id"
		}`))

		Expect(campaignsCollection.CreateCall.Receives.Connection).To(Equal(database.Connection()))
		Expect(campaignsCollection.CreateCall.Receives.Campaign).To(Equal(collections.Campaign{
			SendTo:         map[string]string{"space": "space-123"},
			CampaignTypeID: "some-campaign-type-id",
			Text:           "come see our new stuff",
			HTML:           "<h1>New stuff</h1>",
			Subject:        "Cool New Stuff",
			TemplateID:     "random-template-id",
			ReplyTo:        "reply-to-address",
			SenderID:       "some-sender-id",
			StartTime:      startTime,
		}))
	})

	It("sends a campaign to an org", func() {
		requestBody, err := json.Marshal(map[string]interface{}{