Beispiel #1
0
package models_test

import (
	"github.com/cloudfoundry-incubator/notifications/v1/models"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("Kind", func() {
	var kind models.Kind

	Describe("TemplateToUse", func() {
		Context("when the template is set", func() {
			BeforeEach(func() {
				kind.TemplateID = "template-id"
			})

			It("returns the template value", func() {
				Expect(kind.TemplateToUse()).To(Equal("template-id"))
			})
		})

		Context("when the template is not set", func() {
			BeforeEach(func() {
				kind.TemplateID = ""
			})

			It("returns the default template value", func() {
				Expect(kind.TemplateToUse()).To(Equal(models.DefaultTemplateID))
			})
				Expect(err).To(BeAssignableToTypeOf(models.RecordNotFoundError("")))
			})

			It("reports that the template cannot be found", func() {
				templatesRepo.FindByIDCall.Returns.Error = models.RecordNotFoundError("not found")

				err := assigner.AssignToNotification(database, "my-client", "my-kind", "non-existant-template")
				Expect(err).To(HaveOccurred())
				Expect(err).To(BeAssignableToTypeOf(services.TemplateAssignmentError("")))
			})
		})

		Context("when the request should reset the template assignment", func() {
			BeforeEach(func() {
				var err error
				kind.TemplateID = "some-random-template"
				kind, err = kindsRepo.Update(conn, kind)
				Expect(err).NotTo(HaveOccurred())
			})

			It("allows template id of empty string to reset the assignment", func() {
				err := assigner.AssignToNotification(database, "my-client", "my-kind", "")
				Expect(err).NotTo(HaveOccurred())

				Expect(kindsRepo.UpdateCall.Receives.Kind).To(Equal(models.Kind{
					ID:         "my-kind",
					ClientID:   "my-client",
					TemplateID: models.DefaultTemplateID,
				}))
			})
					ClientID:   "my-client",
					TemplateID: "my-template",
				}

				kind, err := repo.Upsert(conn, kind)
				if err != nil {
					panic(err)
				}

				primary := kind.Primary
				createdAt := kind.CreatedAt

				kind.Description = "My Kind"
				kind.Critical = true
				kind.Primary = 42069
				kind.TemplateID = "new-template"
				kind.CreatedAt = time.Now().Add(-3 * time.Minute)

				kind, err = repo.Update(conn, kind)
				if err != nil {
					panic(err)
				}

				kind, err = repo.Find(conn, "my-kind", "my-client")
				if err != nil {
					panic(err)
				}

				Expect(kind.ID).To(Equal("my-kind"))
				Expect(kind.Description).To(Equal("My Kind"))
				Expect(kind.Critical).To(BeTrue())