1package dns_test
2
3import (
4	"errors"
5
6	gcpdns "google.golang.org/api/dns/v1"
7
8	"github.com/genevieve/leftovers/gcp/dns"
9	"github.com/genevieve/leftovers/gcp/dns/fakes"
10	. "github.com/onsi/ginkgo"
11	. "github.com/onsi/gomega"
12)
13
14var _ = Describe("ManagedZones", func() {
15	var (
16		client     *fakes.ManagedZonesClient
17		recordSets *fakes.RecordSets
18		logger     *fakes.Logger
19
20		managedZones dns.ManagedZones
21	)
22
23	BeforeEach(func() {
24		client = &fakes.ManagedZonesClient{}
25		recordSets = &fakes.RecordSets{}
26		logger = &fakes.Logger{}
27
28		logger.PromptWithDetailsCall.Returns.Proceed = true
29
30		managedZones = dns.NewManagedZones(client, recordSets, logger)
31	})
32
33	Describe("List", func() {
34		var filter string
35
36		BeforeEach(func() {
37			client.ListManagedZonesCall.Returns.Output = &gcpdns.ManagedZonesListResponse{
38				ManagedZones: []*gcpdns.ManagedZone{{
39					Name: "banana-managed-zone",
40				}},
41			}
42			filter = "banana"
43		})
44
45		It("lists, filters, and prompts for managed zones to delete", func() {
46			list, err := managedZones.List(filter)
47			Expect(err).NotTo(HaveOccurred())
48
49			Expect(client.ListManagedZonesCall.CallCount).To(Equal(1))
50
51			Expect(logger.PromptWithDetailsCall.CallCount).To(Equal(1))
52			Expect(logger.PromptWithDetailsCall.Receives.Type).To(Equal("DNS Managed Zone"))
53			Expect(logger.PromptWithDetailsCall.Receives.Name).To(Equal("banana-managed-zone"))
54
55			Expect(list).To(HaveLen(1))
56		})
57
58		Context("when the client fails to list managed zones", func() {
59			BeforeEach(func() {
60				client.ListManagedZonesCall.Returns.Error = errors.New("some error")
61			})
62
63			It("returns the error", func() {
64				_, err := managedZones.List(filter)
65				Expect(err).To(MatchError("Listing DNS Managed Zones: some error"))
66			})
67		})
68
69		Context("when the managed zone name does not contain the filter", func() {
70			It("does not add it to the list", func() {
71				list, err := managedZones.List("grape")
72				Expect(err).NotTo(HaveOccurred())
73
74				Expect(logger.PromptWithDetailsCall.CallCount).To(Equal(0))
75				Expect(list).To(HaveLen(0))
76			})
77		})
78
79		Context("when the user says no to the prompt", func() {
80			BeforeEach(func() {
81				logger.PromptWithDetailsCall.Returns.Proceed = false
82			})
83
84			It("does not add it to the list", func() {
85				list, err := managedZones.List(filter)
86				Expect(err).NotTo(HaveOccurred())
87
88				Expect(list).To(HaveLen(0))
89			})
90		})
91	})
92})
93