Skip to content

Commit

Permalink
MG-2457 - Update adapter tests (#2529)
Browse files Browse the repository at this point in the history
Signed-off-by: Felix Gateru <[email protected]>
  • Loading branch information
felixgateru authored Nov 27, 2024
1 parent 191a107 commit 451765d
Show file tree
Hide file tree
Showing 5 changed files with 659 additions and 121 deletions.
73 changes: 58 additions & 15 deletions http/api/endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@ import (
"github.com/absmach/magistrala/http/api"
grpcChannelsV1 "github.com/absmach/magistrala/internal/grpc/channels/v1"
grpcClientsV1 "github.com/absmach/magistrala/internal/grpc/clients/v1"
"github.com/absmach/magistrala/internal/testsutil"
mglog "github.com/absmach/magistrala/logger"
"github.com/absmach/magistrala/pkg/apiutil"
mgauthn "github.com/absmach/magistrala/pkg/authn"
authnMocks "github.com/absmach/magistrala/pkg/authn/mocks"
"github.com/absmach/magistrala/pkg/connections"
pubsub "github.com/absmach/magistrala/pkg/messaging/mocks"
"github.com/absmach/magistrala/pkg/policies"
"github.com/absmach/mgate"
proxy "github.com/absmach/mgate/pkg/http"
"github.com/absmach/mgate/pkg/session"
Expand All @@ -34,6 +37,8 @@ const (
invalidValue = "invalid"
)

var clientID = testsutil.GenerateUUID(&testing.T{})

func newService(authn mgauthn.Authentication, clients grpcClientsV1.ClientsServiceClient, channels grpcChannelsV1.ChannelsServiceClient) (session.Handler, *pubsub.PubSub) {
pub := new(pubsub.PubSub)
return server.NewHandler(pub, authn, clients, channels, mglog.NewMock()), pub
Expand Down Expand Up @@ -76,7 +81,7 @@ func (tr testRequest) make() (*http.Response, error) {
req.Header.Set("Authorization", apiutil.ClientPrefix+tr.token)
}
if tr.basicAuth && tr.token != "" {
req.SetBasicAuth("", tr.token)
req.SetBasicAuth("", apiutil.ClientPrefix+tr.token)
}
if tr.contentType != "" {
req.Header.Set("Content-Type", tr.contentType)
Expand Down Expand Up @@ -105,83 +110,119 @@ func TestPublish(t *testing.T) {

defer ts.Close()

cases := map[string]struct {
cases := []struct {
desc string
chanID string
msg string
contentType string
key string
status int
basicAuth bool
authnErr error
authnRes *grpcClientsV1.AuthnRes
authzRes *grpcChannelsV1.AuthzRes
authzErr error
err error
}{
"publish message": {
{
desc: "publish message successfully",
chanID: chanID,
msg: msg,
contentType: ctSenmlJSON,
key: clientKey,
status: http.StatusAccepted,
authnRes: &grpcClientsV1.AuthnRes{Id: clientID, Authenticated: true},
authzRes: &grpcChannelsV1.AuthzRes{Authorized: true},
},
"publish message with application/senml+cbor content-type": {
{
desc: "publish message with application/senml+cbor content-type",
chanID: chanID,
msg: msgCBOR,
contentType: ctSenmlCBOR,
key: clientKey,
status: http.StatusAccepted,
authnRes: &grpcClientsV1.AuthnRes{Id: clientID, Authenticated: true},
authzRes: &grpcChannelsV1.AuthzRes{Authorized: true},
},
"publish message with application/json content-type": {
{
desc: "publish message with application/json content-type",
chanID: chanID,
msg: msgJSON,
contentType: ctJSON,
key: clientKey,
status: http.StatusAccepted,
authnRes: &grpcClientsV1.AuthnRes{Id: clientID, Authenticated: true},
authzRes: &grpcChannelsV1.AuthzRes{Authorized: true},
},
"publish message with empty key": {
{
desc: "publish message with empty key",
chanID: chanID,
msg: msg,
contentType: ctSenmlJSON,
key: "",
status: http.StatusBadGateway,
},
"publish message with basic auth": {
{
desc: "publish message with basic auth",
chanID: chanID,
msg: msg,
contentType: ctSenmlJSON,
key: clientKey,
basicAuth: true,
status: http.StatusAccepted,
authnRes: &grpcClientsV1.AuthnRes{Id: clientID, Authenticated: true},
authzRes: &grpcChannelsV1.AuthzRes{Authorized: true},
},
"publish message with invalid key": {
{
desc: "publish message with invalid key",
chanID: chanID,
msg: msg,
contentType: ctSenmlJSON,
key: invalidKey,
status: http.StatusUnauthorized,
authnRes: &grpcClientsV1.AuthnRes{Authenticated: false},
},
"publish message with invalid basic auth": {
{
desc: "publish message with invalid basic auth",
chanID: chanID,
msg: msg,
contentType: ctSenmlJSON,
key: invalidKey,
basicAuth: true,
status: http.StatusUnauthorized,
authnRes: &grpcClientsV1.AuthnRes{Authenticated: false},
},
"publish message without content type": {
{
desc: "publish message without content type",
chanID: chanID,
msg: msg,
contentType: "",
key: clientKey,
status: http.StatusUnsupportedMediaType,
authnRes: &grpcClientsV1.AuthnRes{Id: clientID, Authenticated: true},
authzRes: &grpcChannelsV1.AuthzRes{Authorized: true},
},
"publish message to invalid channel": {
{
desc: "publish message to invalid channel",
chanID: "",
msg: msg,
contentType: ctSenmlJSON,
key: clientKey,
status: http.StatusBadRequest,
authnRes: &grpcClientsV1.AuthnRes{Id: clientID, Authenticated: true},
authzRes: &grpcChannelsV1.AuthzRes{Authorized: false},
},
}

for desc, tc := range cases {
t.Run(desc, func(t *testing.T) {
for _, tc := range cases {
t.Run(tc.desc, func(t *testing.T) {
clientsCall := clients.On("Authenticate", mock.Anything, &grpcClientsV1.AuthnReq{ClientSecret: tc.key}).Return(tc.authnRes, tc.authnErr)
channelsCall := channels.On("Authorize", mock.Anything, &grpcChannelsV1.AuthzReq{
ChannelId: tc.chanID,
ClientId: clientID,
ClientType: policies.ClientType,
Type: uint32(connections.Publish),
}).Return(tc.authzRes, tc.authzErr)
svcCall := pub.On("Publish", mock.Anything, tc.chanID, mock.Anything).Return(nil)
req := testRequest{
client: ts.Client(),
Expand All @@ -193,9 +234,11 @@ func TestPublish(t *testing.T) {
basicAuth: tc.basicAuth,
}
res, err := req.make()
assert.Nil(t, err, fmt.Sprintf("%s: unexpected error %s", desc, err))
assert.Equal(t, tc.status, res.StatusCode, fmt.Sprintf("%s: expected status code %d got %d", desc, tc.status, res.StatusCode))
assert.Nil(t, err, fmt.Sprintf("%s: unexpected error %s", tc.desc, err))
assert.Equal(t, tc.status, res.StatusCode, fmt.Sprintf("%s: expected status code %d got %d", tc.desc, tc.status, res.StatusCode))
svcCall.Unset()
clientsCall.Unset()
channelsCall.Unset()
})
}
}
Loading

0 comments on commit 451765d

Please sign in to comment.