Skip to content

Commit

Permalink
test: Add tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
nzdjb committed Jul 4, 2023
1 parent a400f76 commit 8a2b382
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 4 deletions.
12 changes: 8 additions & 4 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ import (
"github.com/AdamSLevy/jsonrpc2/v14"
)

type IClient interface {
Request(context context.Context, host string, method string, params interface{}, result interface{}) error
}

type Client struct {
Client jsonrpc2.Client
Client IClient
Host string
AccountReference string
APIKey string
Expand All @@ -22,7 +26,7 @@ type ResourceRecord struct {

func NewClient(accountReference string, apiKey string) *Client {
return &Client{
Client: jsonrpc2.Client{},
Client: &jsonrpc2.Client{},
Host: "https://metaname.net/api/1.1",
AccountReference: accountReference,
APIKey: apiKey,
Expand All @@ -37,13 +41,13 @@ func (c *Client) CreateDnsRecord(ctx context.Context, domainName string, record
}

func (c *Client) UpdateDnsRecord(ctx context.Context, domainName string, reference string, record ResourceRecord) error {
params := []interface{}{c.AccountReference, c.APIKey, domainName, reference, record, nil}
params := []interface{}{c.AccountReference, c.APIKey, domainName, reference, record}
err := c.Client.Request(ctx, c.Host, "update_dns_record", params, nil)
return err
}

func (c *Client) DeleteDnsRecord(ctx context.Context, domainName string, reference string) error {
params := []interface{}{c.AccountReference, c.APIKey, domainName, reference, nil}
params := []interface{}{c.AccountReference, c.APIKey, domainName, reference}
err := c.Client.Request(ctx, c.Host, "delete_dns_record", params, nil)
return err
}
Expand Down
149 changes: 149 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
package metaname

import (
"context"
"testing"

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

type FauxClient struct {
requestCalled bool
hostUsed string
methodUsed string
paramsUsed interface{}
}

func (fc *FauxClient) Request(context context.Context, host string, method string, params interface{}, result interface{}) error {
fc.requestCalled = true
fc.hostUsed = host
fc.methodUsed = method
fc.paramsUsed = params
return nil
}

func TestNewClient(t *testing.T) {
c := NewClient("username", "apikey")
assert.Equal(t, "https://metaname.net/api/1.1", c.Host)
assert.Equal(t, "username", c.AccountReference)
assert.Equal(t, "apikey", c.APIKey)
}

func TestDnsZone(t *testing.T) {
c := &Client{
Client: &FauxClient{},
Host: "abc",
AccountReference: "def",
APIKey: "ghi",
}
_, err := c.DnsZone(context.TODO(), "testzone.nz")
if err != nil {
panic(err)
}
assert.True(t, c.Client.(*FauxClient).requestCalled)
assert.Equal(t, "abc", c.Client.(*FauxClient).hostUsed)
assert.Equal(t, "dns_zone", c.Client.(*FauxClient).methodUsed)
assert.Equal(t, []interface{}{"def", "ghi", "testzone.nz"}, c.Client.(*FauxClient).paramsUsed)
}
func TestConfigureZone(t *testing.T) {
c := &Client{
Client: &FauxClient{},
Host: "abc",
AccountReference: "def",
APIKey: "ghi",
}
err := c.ConfigureZone(context.TODO(), "testzone.nz")
if err != nil {
panic(err)
}
assert.True(t, c.Client.(*FauxClient).requestCalled)
assert.Equal(t, "abc", c.Client.(*FauxClient).hostUsed)
assert.Equal(t, "configure_zone", c.Client.(*FauxClient).methodUsed)
assert.Equal(t, []interface{}{"def", "ghi", "testzone.nz", []ResourceRecord{}, nil}, c.Client.(*FauxClient).paramsUsed)
}

func TestCreateDnsRecord(t *testing.T) {
c := &Client{
Client: &FauxClient{},
Host: "abc",
AccountReference: "def",
APIKey: "ghi",
}
record := ResourceRecord{
Name: "testrecord",
Type: "A",
Data: "127.0.0.1",
Ttl: 300,
}
_, err := c.CreateDnsRecord(context.TODO(), "testzone.nz", record)
if err != nil {
panic(err)
}
assert.True(t, c.Client.(*FauxClient).requestCalled)
assert.Equal(t, "abc", c.Client.(*FauxClient).hostUsed)
assert.Equal(t, "create_dns_record", c.Client.(*FauxClient).methodUsed)
assert.Equal(t, []interface{}{"def", "ghi", "testzone.nz", record}, c.Client.(*FauxClient).paramsUsed)
}

func TestCreateMXDnsRecord(t *testing.T) {
c := &Client{
Client: &FauxClient{},
Host: "abc",
AccountReference: "def",
APIKey: "ghi",
}
record := ResourceRecord{
Name: "testrecord",
Type: "MX",
Aux: 30,
Data: "127.0.0.1",
Ttl: 300,
}
_, err := c.CreateDnsRecord(context.TODO(), "testzone.nz", record)
if err != nil {
panic(err)
}
assert.True(t, c.Client.(*FauxClient).requestCalled)
assert.Equal(t, "abc", c.Client.(*FauxClient).hostUsed)
assert.Equal(t, "create_dns_record", c.Client.(*FauxClient).methodUsed)
assert.Equal(t, []interface{}{"def", "ghi", "testzone.nz", record}, c.Client.(*FauxClient).paramsUsed)
}

func TestUpdateDnsRecord(t *testing.T) {
c := &Client{
Client: &FauxClient{},
Host: "abc",
AccountReference: "def",
APIKey: "ghi",
}
record := ResourceRecord{
Name: "testrecord",
Type: "A",
Data: "127.0.0.1",
Ttl: 300,
}
err := c.UpdateDnsRecord(context.TODO(), "testzone.nz", "1234", record)
if err != nil {
panic(err)
}
assert.True(t, c.Client.(*FauxClient).requestCalled)
assert.Equal(t, "abc", c.Client.(*FauxClient).hostUsed)
assert.Equal(t, "update_dns_record", c.Client.(*FauxClient).methodUsed)
assert.Equal(t, []interface{}{"def", "ghi", "testzone.nz", "1234", record}, c.Client.(*FauxClient).paramsUsed)
}
func TestDeleteDnsRecord(t *testing.T) {
c := &Client{
Client: &FauxClient{},
Host: "abc",
AccountReference: "def",
APIKey: "ghi",
}
err := c.DeleteDnsRecord(context.TODO(), "testzone.nz", "1234")
if err != nil {
panic(err)
}
assert.True(t, c.Client.(*FauxClient).requestCalled)
assert.Equal(t, "abc", c.Client.(*FauxClient).hostUsed)
assert.Equal(t, "delete_dns_record", c.Client.(*FauxClient).methodUsed)
assert.Equal(t, []interface{}{"def", "ghi", "testzone.nz", "1234"}, c.Client.(*FauxClient).paramsUsed)
}

0 comments on commit 8a2b382

Please sign in to comment.