blocky/api/api_interface_impl_test.go

217 lines
6.5 KiB
Go

package api
import (
"context"
"errors"
"time"
// . "github.com/0xERR0R/blocky/helpertest"
"github.com/0xERR0R/blocky/model"
"github.com/0xERR0R/blocky/util"
"github.com/miekg/dns"
"github.com/stretchr/testify/mock"
. "github.com/0xERR0R/blocky/helpertest"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
type BlockingControlMock struct {
mock.Mock
}
type ListRefreshMock struct {
mock.Mock
}
type QuerierMock struct {
mock.Mock
}
func (m *ListRefreshMock) RefreshLists() error {
args := m.Called()
return args.Error(0)
}
func (m *BlockingControlMock) EnableBlocking() {
_ = m.Called()
}
func (m *BlockingControlMock) DisableBlocking(t time.Duration, g []string) error {
args := m.Called(t, g)
return args.Error(0)
}
func (m *BlockingControlMock) BlockingStatus() BlockingStatus {
args := m.Called()
return args.Get(0).(BlockingStatus)
}
func (m *QuerierMock) Query(question string, qType dns.Type) (*model.Response, error) {
args := m.Called(question, qType)
return args.Get(0).(*model.Response), args.Error(1)
}
var _ = Describe("API implementation tests", func() {
var (
blockingControlMock *BlockingControlMock
querierMock *QuerierMock
listRefreshMock *ListRefreshMock
sut *OpenAPIInterfaceImpl
)
BeforeEach(func() {
blockingControlMock = &BlockingControlMock{}
querierMock = &QuerierMock{}
listRefreshMock = &ListRefreshMock{}
sut = NewOpenAPIInterfaceImpl(blockingControlMock, querierMock, listRefreshMock)
})
AfterEach(func() {
blockingControlMock.AssertExpectations(GinkgoT())
querierMock.AssertExpectations(GinkgoT())
listRefreshMock.AssertExpectations(GinkgoT())
})
Describe("Query API", func() {
When("Query is called", func() {
It("should return 200 on success", func() {
queryResponse, err := util.NewMsgWithAnswer(
"domain.", 123, A, "0.0.0.0",
)
Expect(err).Should(Succeed())
querierMock.On("Query", "google.com.", A).Return(&model.Response{
Res: queryResponse,
Reason: "reason",
}, nil)
resp, err := sut.Query(context.Background(), QueryRequestObject{
Body: &ApiQueryRequest{
Query: "google.com", Type: "A",
},
})
Expect(err).Should(Succeed())
var resp200 Query200JSONResponse
Expect(resp).Should(BeAssignableToTypeOf(resp200))
resp200 = resp.(Query200JSONResponse)
Expect(resp200.Reason).Should(Equal("reason"))
Expect(resp200.Response).Should(Equal("A (0.0.0.0)"))
Expect(resp200.ResponseType).Should(Equal("RESOLVED"))
Expect(resp200.ReturnCode).Should(Equal("NOERROR"))
})
It("should return 400 on wrong parameter", func() {
resp, err := sut.Query(context.Background(), QueryRequestObject{
Body: &ApiQueryRequest{
Query: "google.com",
Type: "WRONGTYPE",
},
})
Expect(err).Should(Succeed())
var resp400 Query400TextResponse
Expect(resp).Should(BeAssignableToTypeOf(resp400))
Expect(resp).Should(Equal(Query400TextResponse("unknown query type 'WRONGTYPE'")))
})
})
})
Describe("Lists API", func() {
When("List refresh is called", func() {
It("should return 200 on success", func() {
listRefreshMock.On("RefreshLists").Return(nil)
resp, err := sut.ListRefresh(context.Background(), ListRefreshRequestObject{})
Expect(err).Should(Succeed())
var resp200 ListRefresh200Response
Expect(resp).Should(BeAssignableToTypeOf(resp200))
})
It("should return 500 on failure", func() {
listRefreshMock.On("RefreshLists").Return(errors.New("failed"))
resp, err := sut.ListRefresh(context.Background(), ListRefreshRequestObject{})
Expect(err).Should(Succeed())
var resp500 ListRefresh500TextResponse
Expect(resp).Should(BeAssignableToTypeOf(resp500))
Expect(resp).Should(Equal(ListRefresh500TextResponse("failed")))
})
})
})
Describe("Control blocking status via API", func() {
When("Disable blocking is called", func() {
It("should return 200 on success", func() {
blockingControlMock.On("DisableBlocking", 3*time.Second, []string{"gr1", "gr2"}).Return(nil)
duration := "3s"
grroups := "gr1,gr2"
resp, err := sut.DisableBlocking(context.Background(), DisableBlockingRequestObject{
Params: DisableBlockingParams{
Duration: &duration,
Groups: &grroups,
},
})
Expect(err).Should(Succeed())
var resp200 DisableBlocking200Response
Expect(resp).Should(BeAssignableToTypeOf(resp200))
})
It("should return 400 on failure", func() {
blockingControlMock.On("DisableBlocking", mock.Anything, mock.Anything).Return(errors.New("failed"))
resp, err := sut.DisableBlocking(context.Background(), DisableBlockingRequestObject{})
Expect(err).Should(Succeed())
var resp400 DisableBlocking400TextResponse
Expect(resp).Should(BeAssignableToTypeOf(resp400))
Expect(resp).Should(Equal(DisableBlocking400TextResponse("failed")))
})
It("should return 400 on wrong duration parameter", func() {
wrongDuration := "4sds"
resp, err := sut.DisableBlocking(context.Background(), DisableBlockingRequestObject{
Params: DisableBlockingParams{
Duration: &wrongDuration,
},
})
Expect(err).Should(Succeed())
var resp400 DisableBlocking400TextResponse
Expect(resp).Should(BeAssignableToTypeOf(resp400))
Expect(resp).Should(Equal(DisableBlocking400TextResponse("time: unknown unit \"sds\" in duration \"4sds\"")))
})
})
When("Enable blocking is called", func() {
It("should return 200 on success", func() {
blockingControlMock.On("EnableBlocking").Return()
resp, err := sut.EnableBlocking(context.Background(), EnableBlockingRequestObject{})
Expect(err).Should(Succeed())
var resp200 EnableBlocking200Response
Expect(resp).Should(BeAssignableToTypeOf(resp200))
})
})
When("Blocking status is called", func() {
It("should return 200 and correct status", func() {
blockingControlMock.On("BlockingStatus").Return(BlockingStatus{
Enabled: false,
DisabledGroups: []string{"gr1", "gr2"},
AutoEnableInSec: 47,
})
resp, err := sut.BlockingStatus(context.Background(), BlockingStatusRequestObject{})
Expect(err).Should(Succeed())
var resp200 BlockingStatus200JSONResponse
Expect(resp).Should(BeAssignableToTypeOf(resp200))
resp200 = resp.(BlockingStatus200JSONResponse)
Expect(resp200.Enabled).Should(Equal(false))
Expect(resp200.DisabledGroups).Should(HaveValue(Equal([]string{"gr1", "gr2"})))
Expect(resp200.AutoEnableInSec).Should(HaveValue(BeNumerically("==", 47)))
})
})
})
})