Skip to content

Commit

Permalink
feat: add unit tests for the allow and deny lists
Browse files Browse the repository at this point in the history
  • Loading branch information
amalucelli committed Sep 1, 2022
1 parent 2060668 commit 3bc2aa0
Show file tree
Hide file tree
Showing 4 changed files with 202 additions and 1 deletion.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
module github.com/amalucelli/nextdns-go

go 1.18
go 1.19

require (
github.com/hashicorp/go-cleanhttp v0.5.2
github.com/matryer/is v1.4.0
github.com/pkg/errors v0.9.1
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ=
github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48=
github.com/matryer/is v1.4.0 h1:sosSmIWwkYITGrxZ25ULNDeKiMNzFSr4V/eqBQP0PeE=
github.com/matryer/is v1.4.0/go.mod h1:8I/i5uYgLzgsgEloJE1U6xx5HkBQpAZvepWuujKwMRU=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
99 changes: 99 additions & 0 deletions nextdns/allowlist_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package nextdns

import (
"context"
"net/http"
"net/http/httptest"
"testing"

"github.com/matryer/is"
)

func TestAllowlistCreate(t *testing.T) {
c := is.New(t)

ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(204)
_, err := w.Write([]byte(""))
c.NoErr(err)
}))

client, err := New(WithBaseURL(ts.URL))
c.NoErr(err)

ctx := context.Background()
request := &CreateAllowlistRequest{
ProfileID: "abc123",
Allowlist: []*Allowlist{
{
ID: "duckduckgo.com",
Active: true,
},
{
ID: "google.com",
Active: false,
},
},
}
err = client.Allowlist.Create(ctx, request)

c.NoErr(err)
}

func TestAllowlistGet(t *testing.T) {
c := is.New(t)

ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
out := `{"data":[{"id":"google.com","active":false},{"id":"duckduckgo.com","active":true}]}`
_, err := w.Write([]byte(out))
c.NoErr(err)
}))

client, err := New(WithBaseURL(ts.URL))
c.NoErr(err)

ctx := context.Background()

list, err := client.Allowlist.Get(ctx, &GetAllowlistRequest{
ProfileID: "abc123",
})
want := []*Allowlist{
{
ID: "google.com",
Active: false,
},
{
ID: "duckduckgo.com",
Active: true,
},
}

c.NoErr(err)
c.Equal(list, want)
}

func TestAllowlistUpdate(t *testing.T) {
c := is.New(t)

ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(204)
_, err := w.Write([]byte(""))
c.NoErr(err)
}))

client, err := New(WithBaseURL(ts.URL))
c.NoErr(err)

ctx := context.Background()
request := &UpdateAllowlistRequest{
ProfileID: "abc123",
ID: "duckduckgo.com",
Allowlist: &Allowlist{
Active: true,
},
}
err = client.Allowlist.Update(ctx, request)

c.NoErr(err)
}
99 changes: 99 additions & 0 deletions nextdns/denylist_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package nextdns

import (
"context"
"net/http"
"net/http/httptest"
"testing"

"github.com/matryer/is"
)

func TestDenylistCreate(t *testing.T) {
c := is.New(t)

ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(204)
_, err := w.Write([]byte(""))
c.NoErr(err)
}))

client, err := New(WithBaseURL(ts.URL))
c.NoErr(err)

ctx := context.Background()
request := &CreateDenylistRequest{
ProfileID: "abc123",
Denylist: []*Denylist{
{
ID: "whatsapp.net",
Active: true,
},
{
ID: "apple.com",
Active: false,
},
},
}
err = client.Denylist.Create(ctx, request)

c.NoErr(err)
}

func TestDenylistGet(t *testing.T) {
c := is.New(t)

ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
out := `{"data":[{"id":"whatsapp.net","active":true},{"id":"apple.com","active":false}]}`
_, err := w.Write([]byte(out))
c.NoErr(err)
}))

client, err := New(WithBaseURL(ts.URL))
c.NoErr(err)

ctx := context.Background()

list, err := client.Denylist.Get(ctx, &GetDenylistRequest{
ProfileID: "abc123",
})
want := []*Denylist{
{
ID: "whatsapp.net",
Active: true,
},
{
ID: "apple.com",
Active: false,
},
}

c.NoErr(err)
c.Equal(list, want)
}

func TestDenylistUpdate(t *testing.T) {
c := is.New(t)

ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(204)
_, err := w.Write([]byte(""))
c.NoErr(err)
}))

client, err := New(WithBaseURL(ts.URL))
c.NoErr(err)

ctx := context.Background()
request := &UpdateDenylistRequest{
ProfileID: "abc123",
ID: "apple.com",
Denylist: &Denylist{
Active: true,
},
}
err = client.Denylist.Update(ctx, request)

c.NoErr(err)
}

0 comments on commit 3bc2aa0

Please sign in to comment.