aboutsummaryrefslogtreecommitdiffhomepage
path: root/mailgun/resource_mailgun_domain_test.go
blob: c35df48ad32ce16d9bd3b4a7a1f4a6f82b00a5da (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package mailgun

import (
	"context"
	"fmt"
	"github.com/hashicorp/terraform/helper/resource"
	"github.com/hashicorp/terraform/terraform"
	"github.com/mailgun/mailgun-go"
	"os"
	"strconv"
	"testing"
	"time"
)

type fullDomain struct {
	domainResponse   mailgun.DomainResponse
	domainConnection mailgun.DomainConnection
	domainTracking   mailgun.DomainTracking
	ipAddress        []string
	credentials      []mailgun.Credential
}

func getFullDomain(mg *mailgun.MailgunImpl, domainName string) (*fullDomain, error) {
	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
	defer cancel()
	mg = mailgun.NewMailgun(domainName, mg.APIKey())

	var domain fullDomain
	var err error
	domain.domainResponse, err = mg.GetDomain(ctx, domainName)
	if err != nil {
		return nil, fmt.Errorf("Error Getting mailgun domain Details for %s: Error: %s", domainName, err)
	}

	domain.domainConnection, err = mg.GetDomainConnection(ctx, domainName)
	if err != nil {
		return nil, fmt.Errorf("Error Getting mailgun domain connection Details for %s: Error: %s", domainName, err)
	}

	domain.domainTracking, err = mg.GetDomainTracking(ctx, domainName)
	if err != nil {
		return nil, fmt.Errorf("Error Getting mailgun domain tracking Details for %s: Error: %s", domainName, err)
	}

	ipAddress, err := mg.ListDomainIPS(ctx)
	if err != nil {
		return nil, fmt.Errorf("Error Getting mailgun domain ips2 for %s: Error: %s", domainName, err)
	}
	ips := make([]string, len(ipAddress))
	for i, r := range ipAddress {
		ips[i] = r.IP

	}
	domain.ipAddress = ips
	domain.credentials, err = ListCredentials(domainName, mg.APIKey())
	if err != nil {
		return nil, fmt.Errorf("Error Getting mailgun credentials for %s: Error: %s", domainName, err)
	}
	return &domain, nil
}

func TestAccMailgunDomain_basic(t *testing.T) {
	var domain fullDomain

	resource.Test(t, resource.TestCase{
		PreCheck:     func() { testAccPreCheck(t) },
		Providers:    testAccProviders,
		CheckDestroy: testAccDomainCheckDestroy(&domain),
		Steps: []resource.TestStep{
			{
				Config: interpolateTerraformTemplateDomain(testAccDomainConfig_basic),
				Check: resource.ComposeTestCheckFunc(
					testAccDomainCheckExists("mailgun_domain.exemple", &domain),
					testAccDomainCheckAttributes("mailgun_domain.exemple", &domain),
				),
			},
		},
	})
}

func TestAccMailgunDomain_withUpdate(t *testing.T) {
	var domain fullDomain

	resource.Test(t, resource.TestCase{
		PreCheck:     func() { testAccPreCheck(t) },
		Providers:    testAccProviders,
		CheckDestroy: testAccDomainCheckDestroy(&domain),
		Steps: []resource.TestStep{
			{
				Config: interpolateTerraformTemplateDomain(testAccDomainConfig_basic),
				Check: resource.ComposeTestCheckFunc(
					testAccDomainCheckExists("mailgun_domain.exemple", &domain),
					testAccDomainCheckAttributes("mailgun_domain.exemple", &domain),
				),
			},

			{
				Config: interpolateTerraformTemplateDomain(testAccDomainConfig_update),
				Check: resource.ComposeTestCheckFunc(
					testAccDomainCheckExists("mailgun_domain.exemple", &domain),
					testAccDomainCheckAttributes("mailgun_domain.exemple", &domain),
				),
			},
		},
	})
}

func testAccDomainCheckExists(rn string, domain *fullDomain) resource.TestCheckFunc {
	return func(s *terraform.State) error {
		rs, ok := s.RootModule().Resources[rn]
		if !ok {
			return fmt.Errorf("resource not found: %s", rn)
		}

		if rs.Primary.ID == "" {
			return fmt.Errorf("domainID not set")
		}

		mg := testAccProvider.Meta().(*mailgun.MailgunImpl)

		domainId := rs.Primary.ID

		gotDomain, err := getFullDomain(mg, domainId)
		if err != nil {
			return fmt.Errorf("error getting domain: %s", err)
		}

		*domain = *gotDomain

		return nil
	}
}

func testAccDomainCheckAttributes(rn string, domain *fullDomain) resource.TestCheckFunc {
	return func(s *terraform.State) error {
		attrs := s.RootModule().Resources[rn].Primary.Attributes

		check := func(key, stateValue, domainValue string) error {
			if domainValue != stateValue {
				return fmt.Errorf("different values for %s in state (%s) and in mailgun (%s)",
					key, stateValue, domainValue)
			}
			return nil
		}

		for key, value := range attrs {
			var err error

			switch key {
			case "name":
				err = check(key, value, domain.domainResponse.Domain.Name)
			case "smtp_password":
				err = check(key, value, domain.domainResponse.Domain.SMTPPassword)
			case "smtp_login":
				err = check(key, value, domain.domainResponse.Domain.SMTPLogin)
			case "wildcard":
				err = check(key, value, strconv.FormatBool(domain.domainResponse.Domain.Wildcard))
			case "state":
				err = check(key, value, domain.domainResponse.Domain.State)
			case "open_tracking_settings_active":
				err = check(key, value, strconv.FormatBool(domain.domainTracking.Open.Active))
			case "click_tracking_settings_active":
				err = check(key, value, strconv.FormatBool(domain.domainTracking.Click.Active))
			case "unsubscribe_tracking_settings_active":
				err = check(key, value, strconv.FormatBool(domain.domainTracking.Unsubscribe.Active))
			case "unsubscribe_tracking_settings_html_footer":
				err = check(key, value, domain.domainTracking.Unsubscribe.HTMLFooter)
			case "unsubscribe_tracking_settings_text_footer":
				err = check(key, value, domain.domainTracking.Unsubscribe.TextFooter)
			case "skip_verification":
				err = check(key, value, strconv.FormatBool(domain.domainConnection.SkipVerification))
			case "require_tls":
				err = check(key, value, strconv.FormatBool(domain.domainConnection.RequireTLS))
			}
			if err != nil {
				return err
			}
		}
		return nil
	}
}

func testAccDomainCheckDestroy(domain *fullDomain) resource.TestCheckFunc {
	return func(s *terraform.State) error {
		mg := testAccProvider.Meta().(*mailgun.MailgunImpl)
		ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
		defer cancel()

		_, err := mg.GetDomain(ctx, domain.domainResponse.Domain.Name)
		if err == nil {
			return fmt.Errorf("domain still exists")
		}

		return nil
	}
}

func interpolateTerraformTemplateDomain(template string) string {
	domainName := ""

	if v := os.Getenv("MAILGUN_DOMAIN"); v != "" {
		domainName = v
	}

	return fmt.Sprintf(template, domainName)
}

const testAccDomainConfig_basic = `
resource "mailgun_domain" "exemple" {
	name="%s"
        wildcard=true
        credentials{
             login="aaaaaaa"
             password="adfshfjqdskjhgfksdgfkqgfk"
        }
}
`

const testAccDomainConfig_update = `
resource "mailgun_domain" "exemple" {
	name="%s"
        credentials{
             login="aaaaaaa"
             password="adfshfjqdskjhgfksdgfkqgfk"
        }

}
`