Example #1
0
				Expect(kind.Critical).To(BeTrue())
				Expect(kind.ClientID).To(Equal("my-client"))
				Expect(kind.TemplateID).To(Equal("new-template"))
				Expect(kind.UpdatedAt).To(BeTemporally("~", time.Now(), 2*time.Second))
				Expect(kind.CreatedAt).To(Equal(createdAt))
				Expect(kind.Primary).To(Equal(primary))
			})

			It("returns a record not found error when the record does not exist", func() {
				kind := models.Kind{
					ID:         "my-kind",
					ClientID:   "my-client",
					TemplateID: "my-template",
				}
				_, err := repo.Update(conn, kind)
				Expect(err).To(BeAssignableToTypeOf(models.RecordNotFoundError("")))
			})
		})

		Context("when the template id is not meant to be set", func() {
			It("updates the record in the database, using the existing template ID", func() {
				kind := models.Kind{
					ID:         "my-kind",
					ClientID:   "my-client",
					TemplateID: "my-template",
				}

				kind, err := repo.Upsert(conn, kind)
				if err != nil {
					panic(err)
				}
			err := assigner.AssignToClient(database, "my-client", "my-template")
			Expect(err).NotTo(HaveOccurred())

			Expect(clientsRepo.FindCall.Receives.Connection).To(Equal(conn))
			Expect(clientsRepo.FindCall.Receives.ClientID).To(Equal("my-client"))

			Expect(clientsRepo.UpdateCall.Receives.Connection).To(Equal(conn))
			Expect(clientsRepo.UpdateCall.Receives.Client).To(Equal(models.Client{
				ID:         "my-client",
				TemplateID: "my-template",
			}))
		})

		Context("when the request includes a non-existant id", func() {
			It("reports that the client cannot be found", func() {
				clientsRepo.FindCall.Returns.Error = models.RecordNotFoundError("not found")

				err := assigner.AssignToClient(database, "missing-client", "my-template")
				Expect(err).To(HaveOccurred())
				Expect(err).To(BeAssignableToTypeOf(models.RecordNotFoundError("not found")))
			})

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

				err := assigner.AssignToClient(database, "my-client", "non-existant-template")
				Expect(err).To(HaveOccurred())
				Expect(err).To(BeAssignableToTypeOf(services.TemplateAssignmentError("")))
			})
		})
		It("retrieves clients and kinds from the database", func() {
			client, kind, err := finder.ClientAndKind(database, "some-client-id", "perimeter_breach")
			Expect(err).NotTo(HaveOccurred())
			Expect(client).To(Equal(models.Client{
				ID: "some-client-id",
			}))
			Expect(kind).To(Equal(models.Kind{
				ID:       "perimeter_breach",
				ClientID: "some-client-id",
			}))
		})

		Context("when the client cannot be found", func() {
			It("returns an empty models.Client", func() {
				clientsRepo.FindCall.Returns.Error = models.RecordNotFoundError("not found")

				client, _, err := finder.ClientAndKind(database, "missing-client-id", "perimeter_breach")
				Expect(err).NotTo(HaveOccurred())
				Expect(client).To(Equal(models.Client{
					ID: "missing-client-id",
				}))
			})
		})

		Context("when the kind cannot be found", func() {
			It("returns an empty models.Kind", func() {
				kindsRepo.FindCall.Returns.Error = models.RecordNotFoundError("")

				client, kind, err := finder.ClientAndKind(database, "some-client-id", "bad-kind-id")
				Expect(err).NotTo(HaveOccurred())