File: services_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 (103 lines) | stat: -rw-r--r-- 2,828 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
package dnsimple

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

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

func TestServicePath(t *testing.T) {
	assert.Equal(t, "/services", servicePath(""))
	assert.Equal(t, "/services/name", servicePath("name"))
}

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

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

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

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

	servicesResponse, err := client.Services.ListServices(context.Background(), nil)

	assert.NoError(t, err)
	assert.Equal(t, &Pagination{CurrentPage: 1, PerPage: 30, TotalPages: 1, TotalEntries: 2}, servicesResponse.Pagination)
	services := servicesResponse.Data
	assert.Len(t, services, 2)
	assert.Equal(t, int64(1), services[0].ID)
	assert.Equal(t, "Service 1", services[0].Name)
}

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

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

		testQuery(t, r, url.Values{"page": []string{"2"}, "per_page": []string{"20"}})

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

	_, err := client.Services.ListServices(context.Background(), &ListOptions{Page: Int(2), PerPage: Int(20)})

	assert.NoError(t, err)
}

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

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

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

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

	serviceID := "1"

	serviceResponse, err := client.Services.GetService(context.Background(), serviceID)

	assert.NoError(t, err)
	service := serviceResponse.Data
	wantSingle := &Service{
		ID:               1,
		SID:              "service1",
		Name:             "Service 1",
		Description:      "First service example.",
		SetupDescription: "",
		RequiresSetup:    true,
		DefaultSubdomain: "",
		CreatedAt:        "2014-02-14T19:15:19Z",
		UpdatedAt:        "2016-03-04T09:23:27Z",
		Settings: []ServiceSetting{
			{
				Name:        "username",
				Label:       "Service 1 Account Username",
				Append:      ".service1.com",
				Description: "Your Service 1 username is used to connect services to your account.",
				Example:     "username",
				Password:    false,
			},
		},
	}
	assert.Equal(t, wantSingle, service)
}