File: client_test.go

package info (click to toggle)
golang-github-xenolf-lego 4.9.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,080 kB
  • sloc: xml: 533; makefile: 128; sh: 18
file content (185 lines) | stat: -rw-r--r-- 4,216 bytes parent folder | download | duplicates (2)
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
package internal

import (
	"fmt"
	"io"
	"net/http"
	"net/http/httptest"
	"net/url"
	"os"
	"testing"

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

func setup(t *testing.T) (*Client, *http.ServeMux) {
	t.Helper()

	mux := http.NewServeMux()
	server := httptest.NewServer(mux)
	t.Cleanup(server.Close)

	client := NewClient("secret")
	client.baseURL, _ = url.Parse(server.URL)

	return client, mux
}

func mockHandler(method string, filename string) http.HandlerFunc {
	return func(rw http.ResponseWriter, req *http.Request) {
		if req.Method != method {
			http.Error(rw, fmt.Sprintf("invalid method, got %s want %s", req.Method, method), http.StatusBadRequest)
			return
		}

		filename = "./fixtures/" + filename
		statusCode := http.StatusOK

		if req.Header.Get("Authorization") != "token secret" {
			statusCode = http.StatusUnauthorized
			filename = "./fixtures/error.json"
		}

		rw.WriteHeader(statusCode)

		file, err := os.Open(filename)
		if err != nil {
			http.Error(rw, err.Error(), http.StatusInternalServerError)
			return
		}
		defer func() { _ = file.Close() }()

		_, err = io.Copy(rw, file)
		if err != nil {
			http.Error(rw, err.Error(), http.StatusInternalServerError)
			return
		}
	}
}

func TestClient_CreateDNSRecord(t *testing.T) {
	client, mux := setup(t)

	mux.HandleFunc("/dns-records", mockHandler(http.MethodPost, "POST_dns-records.json"))

	record := DNSRecord{
		RecordType: "TXT",
		Name:       "_acme-challenge",
		Domain:     "example.com",
		Data:       "test",
		TTL:        300,
	}

	resp, err := client.CreateDNSRecord(record)
	require.NoError(t, err)

	expected := &CreateDNSRecordResponse{
		Data: struct {
			Type       string `json:"type"`
			ID         string `json:"id"`
			Attributes struct {
				Status string `json:"status"`
			} `json:"attributes"`
			Links struct {
				QueueJob  string `json:"queue-job"`
				DNSRecord string `json:"dns-record"`
			} `json:"links"`
		}{
			Type: "queue-job",
			ID:   "18181818",
			Attributes: struct {
				Status string `json:"status"`
			}{
				Status: "pending",
			},
			Links: struct {
				QueueJob  string `json:"queue-job"`
				DNSRecord string `json:"dns-record"`
			}{
				QueueJob:  "https://api.variomedia.de/queue-jobs/18181818",
				DNSRecord: "https://api.variomedia.de/dns-records/19191919",
			},
		},
	}

	assert.Equal(t, expected, resp)
}

func TestClient_DeleteDNSRecord(t *testing.T) {
	client, mux := setup(t)

	mux.HandleFunc("/dns-records/test", mockHandler(http.MethodDelete, "DELETE_dns-records_pending.json"))

	resp, err := client.DeleteDNSRecord("test")
	require.NoError(t, err)

	expected := &DeleteRecordResponse{
		Data: struct {
			ID         string `json:"id"`
			Type       string `json:"type"`
			Attributes struct {
				JobType string `json:"job_type"`
				Status  string `json:"status"`
			} `json:"attributes"`
			Links struct {
				Self   string `json:"self"`
				Object string `json:"object"`
			} `json:"links"`
		}{
			ID:   "303030",
			Type: "queue-job",
			Attributes: struct {
				JobType string `json:"job_type"`
				Status  string `json:"status"`
			}{
				Status: "pending",
			},
		},
	}

	assert.Equal(t, expected, resp)
}

func TestClient_GetJob(t *testing.T) {
	client, mux := setup(t)

	mux.HandleFunc("/queue-jobs/test", mockHandler(http.MethodGet, "GET_queue-jobs.json"))

	resp, err := client.GetJob("test")
	require.NoError(t, err)

	expected := &GetJobResponse{
		Data: struct {
			ID         string `json:"id"`
			Type       string `json:"type"`
			Attributes struct {
				JobType string `json:"job_type"`
				Status  string `json:"status"`
			} `json:"attributes"`
			Links struct {
				Self   string `json:"self"`
				Object string `json:"object"`
			} `json:"links"`
		}{
			ID:   "171717",
			Type: "queue-job",
			Attributes: struct {
				JobType string `json:"job_type"`
				Status  string `json:"status"`
			}{
				JobType: "dns-record",
				Status:  "done",
			},
			Links: struct {
				Self   string `json:"self"`
				Object string `json:"object"`
			}{
				Self:   "https://api.variomedia.de/queue-jobs/171717",
				Object: "https://api.variomedia.de/dns-records/212121",
			},
		},
	}

	assert.Equal(t, expected, resp)
}