File: domains_test.go

package info (click to toggle)
golang-github-dnsimple-dnsimple-go 2.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,428 kB
  • sloc: makefile: 3
file content (150 lines) | stat: -rw-r--r-- 4,352 bytes parent folder | download
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
package dnsimple

import (
	"context"
	"io"
	"net/http"
	"net/url"
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestDomainPath(t *testing.T) {
	assert.Equal(t, "/1010/domains", domainPath("1010", ""))
	assert.Equal(t, "/1010/domains/example.com", domainPath("1010", "example.com"))
}

func TestDomainsService_ListDomains(t *testing.T) {
	setupMockServer()
	defer teardownMockServer()

	mux.HandleFunc("/v2/1385/domains", func(w http.ResponseWriter, r *http.Request) {
		httpResponse := httpResponseFixture(t, "/api/listDomains/success.http")

		testMethod(t, r, "GET")
		testHeaders(t, r)
		testQuery(t, r, url.Values{})

		w.WriteHeader(httpResponse.StatusCode)
		_, _ = io.Copy(w, httpResponse.Body)
	})

	domainsResponse, err := client.Domains.ListDomains(context.Background(), "1385", nil)

	assert.NoError(t, err)
	assert.Equal(t, &Pagination{CurrentPage: 1, PerPage: 30, TotalPages: 1, TotalEntries: 2}, domainsResponse.Pagination)
	domains := domainsResponse.Data
	assert.Len(t, domains, 2)
	assert.Equal(t, int64(181984), domains[0].ID)
	assert.Equal(t, "example-alpha.com", domains[0].Name)
	assert.Equal(t, "2021-06-05T02:15:00Z", domains[0].ExpiresAt)
}

func TestDomainsService_ListDomains_WithOptions(t *testing.T) {
	setupMockServer()
	defer teardownMockServer()

	mux.HandleFunc("/v2/1010/domains", func(w http.ResponseWriter, r *http.Request) {
		httpResponse := httpResponseFixture(t, "/api/listDomains/success.http")

		testQuery(t, r, url.Values{
			"page":          []string{"2"},
			"per_page":      []string{"20"},
			"sort":          []string{"name,expiration:desc"},
			"name_like":     []string{"example"},
			"registrant_id": []string{"10"},
		})

		w.WriteHeader(httpResponse.StatusCode)
		_, _ = io.Copy(w, httpResponse.Body)
	})

	_, err := client.Domains.ListDomains(context.Background(), "1010", &DomainListOptions{NameLike: String("example"), RegistrantID: Int(10), ListOptions: ListOptions{Page: Int(2), PerPage: Int(20), Sort: String("name,expiration:desc")}})

	assert.NoError(t, err)
}

func TestDomainsService_CreateDomain(t *testing.T) {
	setupMockServer()
	defer teardownMockServer()

	mux.HandleFunc("/v2/1385/domains", func(w http.ResponseWriter, r *http.Request) {
		httpResponse := httpResponseFixture(t, "/api/createDomain/created.http")

		testMethod(t, r, "POST")
		testHeaders(t, r)

		want := map[string]interface{}{"name": "example-beta.com"}
		testRequestJSON(t, r, want)

		w.WriteHeader(httpResponse.StatusCode)
		_, _ = io.Copy(w, httpResponse.Body)
	})

	accountID := "1385"
	domainAttributes := Domain{Name: "example-beta.com"}

	domainResponse, err := client.Domains.CreateDomain(context.Background(), accountID, domainAttributes)

	assert.NoError(t, err)
	domain := domainResponse.Data
	assert.Equal(t, int64(181985), domain.ID)
}

func TestDomainsService_GetDomain(t *testing.T) {
	setupMockServer()
	defer teardownMockServer()

	mux.HandleFunc("/v2/1010/domains/example-alpha.com", func(w http.ResponseWriter, r *http.Request) {
		httpResponse := httpResponseFixture(t, "/api/getDomain/success.http")

		testMethod(t, r, "GET")
		testHeaders(t, r)

		w.WriteHeader(httpResponse.StatusCode)
		_, _ = io.Copy(w, httpResponse.Body)
	})

	accountID := "1010"

	domainResponse, err := client.Domains.GetDomain(context.Background(), accountID, "example-alpha.com")

	assert.NoError(t, err)
	domain := domainResponse.Data
	wantSingle := &Domain{
		ID:           181984,
		AccountID:    1385,
		RegistrantID: 2715,
		Name:         "example-alpha.com",
		UnicodeName:  "example-alpha.com",
		Token:        "",
		State:        "registered",
		AutoRenew:    false,
		PrivateWhois: false,
		ExpiresAt:    "2021-06-05T02:15:00Z",
		CreatedAt:    "2020-06-04T19:15:14Z",
		UpdatedAt:    "2020-06-04T19:15:21Z"}
	assert.Equal(t, wantSingle, domain)
}

func TestDomainsService_DeleteDomain(t *testing.T) {
	setupMockServer()
	defer teardownMockServer()

	mux.HandleFunc("/v2/1010/domains/example.com", func(w http.ResponseWriter, r *http.Request) {
		httpResponse := httpResponseFixture(t, "/api/deleteDomain/success.http")

		testMethod(t, r, "DELETE")
		testHeaders(t, r)

		w.WriteHeader(httpResponse.StatusCode)
		_, _ = io.Copy(w, httpResponse.Body)
	})

	accountID := "1010"

	_, err := client.Domains.DeleteDomain(context.Background(), accountID, "example.com")

	assert.NoError(t, err)
}