-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add unit tests for the allow and deny lists
- Loading branch information
1 parent
2060668
commit 3bc2aa0
Showing
4 changed files
with
202 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |