Skip to content

Commit f141a44

Browse files
committed
tests: update tests
Signed-off-by: Felix Gateru <[email protected]>
1 parent e40177e commit f141a44

File tree

17 files changed

+1062
-693
lines changed

17 files changed

+1062
-693
lines changed

.github/workflows/tests.yaml

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -240,14 +240,6 @@ jobs:
240240
- "pkg/uuid/**"
241241
- "pkg/events/**"
242242
243-
ws:
244-
- "ws/**"
245-
- "cmd/ws/**"
246-
- "auth.pb.go"
247-
- "auth_grpc.pb.go"
248-
- "clients/**"
249-
- "pkg/messaging/**"
250-
251243
notifications:
252244
- "notifications/**"
253245
- "cmd/notifications/**"
@@ -272,7 +264,7 @@ jobs:
272264
273265
if [[ "${{ steps.changes.outputs.workflow }}" == "true" ]]; then
274266
# If workflow changed, test everything
275-
modules=("auth" "channels" "cli" "clients" "coap" "domains" "groups" "http" "internal" "journal" "logger" "mqtt" "pkg-errors" "pkg-events" "pkg-grpcclient" "pkg-messaging" "pkg-sdk" "pkg-transformers" "pkg-ulid" "pkg-uuid" "users" "ws" "notifications" "api" "consumers" "readers")
267+
modules=("auth" "channels" "cli" "clients" "coap" "domains" "groups" "http" "internal" "journal" "logger" "mqtt" "pkg-errors" "pkg-events" "pkg-grpcclient" "pkg-messaging" "pkg-sdk" "pkg-transformers" "pkg-ulid" "pkg-uuid" "users" "notifications" "api" "consumers" "readers")
276268
else
277269
# Add only changed modules
278270
[[ "${{ steps.changes.outputs.auth }}" == "true" ]] && modules+=("auth")
@@ -296,7 +288,6 @@ jobs:
296288
[[ "${{ steps.changes.outputs.pkg-ulid }}" == "true" ]] && modules+=("pkg-ulid")
297289
[[ "${{ steps.changes.outputs.pkg-uuid }}" == "true" ]] && modules+=("pkg-uuid")
298290
[[ "${{ steps.changes.outputs.users }}" == "true" ]] && modules+=("users")
299-
[[ "${{ steps.changes.outputs.ws }}" == "true" ]] && modules+=("ws")
300291
[[ "${{ steps.changes.outputs.notifications }}" == "true" ]] && modules+=("notifications")
301292
[[ "${{ steps.changes.outputs.api }}" == "true" ]] && modules+=("api")
302293
[[ "${{ steps.changes.outputs.consumers }}" == "true" ]] && modules+=("consumers")

cmd/http/main.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ func main() {
209209
exitCode = 1
210210
return
211211
}
212-
svc := newService(clientsClient, channelsClient, nps, logger, tracer)
212+
svc := newService(clientsClient, channelsClient, authn, nps, logger, tracer)
213213

214214
targetServerCfg := server.Config{Port: targetHTTPPort}
215215

@@ -237,27 +237,27 @@ func main() {
237237
}
238238
}
239239

240-
func newHandler(pub messaging.Publisher, authn smqauthn.Authentication, cacheCfg messaging.CacheConfig, clients grpcClientsV1.ClientsServiceClient, channels grpcChannelsV1.ChannelsServiceClient, domains grpcDomainsV1.DomainsServiceClient, logger *slog.Logger, tracer trace.Tracer) (session.Handler, error) {
240+
func newHandler(pubsub messaging.PubSub, authn smqauthn.Authentication, cacheCfg messaging.CacheConfig, clients grpcClientsV1.ClientsServiceClient, channels grpcChannelsV1.ChannelsServiceClient, domains grpcDomainsV1.DomainsServiceClient, logger *slog.Logger, tracer trace.Tracer) (session.Handler, error) {
241241
parser, err := messaging.NewTopicParser(cacheCfg, channels, domains)
242242
if err != nil {
243243
return nil, err
244244
}
245-
svc := adapter.NewHandler(pub, authn, clients, channels, parser, logger)
246-
svc = handler.NewTracing(tracer, svc)
247-
svc = handler.NewLogging(svc, logger)
245+
h := adapter.NewHandler(pubsub, logger, authn, clients, channels, parser)
246+
h = handler.NewTracing(tracer, h)
247+
h = handler.NewLogging(h, logger)
248248
counter, latency := prometheus.MakeMetrics(svcName, "handler")
249-
h = handler.MetricsMiddleware(h, counter, latency)
249+
h = handler.NewMetrics(h, counter, latency)
250250

251251
return h, nil
252252
}
253253

254-
func newService(clientsClient grpcClientsV1.ClientsServiceClient, channels grpcChannelsV1.ChannelsServiceClient, nps messaging.PubSub, logger *slog.Logger, tracer trace.Tracer) adapter.Service {
255-
svc := adapter.NewService(clientsClient, channels, nps)
256-
svc = middleware.Tracing(tracer, svc)
257-
svc = middleware.Logging(svc, logger)
254+
func newService(clientsClient grpcClientsV1.ClientsServiceClient, channels grpcChannelsV1.ChannelsServiceClient, authn smqauthn.Authentication, nps messaging.PubSub, logger *slog.Logger, tracer trace.Tracer) adapter.Service {
255+
svc := adapter.NewService(clientsClient, channels, authn, nps)
256+
svc = middleware.NewTracing(tracer, svc)
257+
svc = middleware.NewLogging(svc, logger)
258258
counter, latency := prometheus.MakeMetrics(svcName, "api")
259-
svc = handler.NewMetrics(svc, counter, latency)
260-
return svc, nil
259+
svc = middleware.NewMetrics(svc, counter, latency)
260+
return svc
261261
}
262262

263263
func proxyHTTP(ctx context.Context, cfg server.Config, logger *slog.Logger, sessionHandler session.Handler) error {

http/README.md

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,51 @@
11
# HTTP Adapter
22

3-
The HTTP Adapter exposes HTTP endpoints for publishing messages into SuperMQ channels. It authenticates clients via tokens or Basic auth, resolves domains/channels over gRPC, and forwards payloads to the message broker.
3+
The HTTP Adapter exposes HTTP endpoints for publishing messages and WebSocket capabilities for publishing and subscribing to messages from SuperMQ channels. It authenticates clients via tokens or Basic auth, resolves domains/channels over gRPC, and forwards payloads to the message broker.
44

55
For more on SuperMQ, see the [official documentation][doc].
66

77
## Configuration
88

99
Environment variables (unset values fall back to defaults):
1010

11-
| Variable | Description | Default |
12-
| -------------------------------------- | ------------------------------------------------------------------------------------------ | ------------------------------------- |
13-
| `SMQ_HTTP_ADAPTER_LOG_LEVEL` | Log level (debug, info, warn, error) | debug |
14-
| `SMQ_HTTP_ADAPTER_HOST` | HTTP Adapter host | http-adapter |
15-
| `SMQ_HTTP_ADAPTER_PORT` | HTTP Adapter port | 8008 |
16-
| `SMQ_HTTP_ADAPTER_SERVER_CERT` | Path to PEM-encoded server certificate (enables TLS) | "" |
17-
| `SMQ_HTTP_ADAPTER_SERVER_KEY` | Path to PEM-encoded server key | "" |
18-
| `SMQ_HTTP_ADAPTER_SERVER_CA_CERTS` | Trusted CA bundle for HTTPS server | "" |
19-
| `SMQ_HTTP_ADAPTER_CLIENT_CA_CERTS` | Client CA bundle to require mTLS on HTTPS server | "" |
20-
| `SMQ_HTTP_ADAPTER_CACHE_NUM_COUNTERS` | Cache counters for topic parsing | 200000 |
21-
| `SMQ_HTTP_ADAPTER_CACHE_MAX_COST` | Maximum cache size (bytes) | 1048576 |
22-
| `SMQ_HTTP_ADAPTER_CACHE_BUFFER_ITEMS` | Cache buffer items | 64 |
23-
| `SMQ_MESSAGE_BROKER_URL` | Message broker URL (publishing target) | nats://nats:4222 |
24-
| `SMQ_ES_URL` | Event store URL (publishing middleware) | nats://nats:4222 |
25-
| `SMQ_JAEGER_URL` | Jaeger tracing endpoint | <http://jaeger:4318/v1/traces> |
26-
| `SMQ_JAEGER_TRACE_RATIO` | Trace sampling ratio | 1.0 |
27-
| `SMQ_SEND_TELEMETRY` | Send telemetry to SuperMQ call-home server | true |
28-
| `SMQ_HTTP_ADAPTER_INSTANCE_ID` | Service instance ID (auto-generated when empty) | "" |
29-
| `SMQ_CLIENTS_GRPC_URL` | Clients service gRPC URL | clients:7006 |
30-
| `SMQ_CLIENTS_GRPC_TIMEOUT` | Clients gRPC request timeout | 300s |
31-
| `SMQ_CLIENTS_GRPC_CLIENT_CERT` | Clients gRPC client certificate | "" |
32-
| `SMQ_CLIENTS_GRPC_CLIENT_KEY` | Clients gRPC client key | "" |
33-
| `SMQ_CLIENTS_GRPC_SERVER_CA_CERTS` | Clients gRPC trusted CA bundle | "" |
34-
| `SMQ_CHANNELS_GRPC_URL` | Channels service gRPC URL | channels:7005 |
35-
| `SMQ_CHANNELS_GRPC_TIMEOUT` | Channels gRPC request timeout | 300s |
36-
| `SMQ_CHANNELS_GRPC_CLIENT_CERT` | Channels gRPC client certificate | "" |
37-
| `SMQ_CHANNELS_GRPC_CLIENT_KEY` | Channels gRPC client key | "" |
38-
| `SMQ_CHANNELS_GRPC_SERVER_CA_CERTS` | Channels gRPC trusted CA bundle | "" |
39-
| `SMQ_DOMAINS_GRPC_URL` | Domains service gRPC URL | domains:7003 |
40-
| `SMQ_DOMAINS_GRPC_TIMEOUT` | Domains gRPC request timeout | 300s |
41-
| `SMQ_DOMAINS_GRPC_CLIENT_CERT` | Domains gRPC client certificate | "" |
42-
| `SMQ_DOMAINS_GRPC_CLIENT_KEY` | Domains gRPC client key | "" |
43-
| `SMQ_DOMAINS_GRPC_SERVER_CA_CERTS` | Domains gRPC trusted CA bundle | "" |
44-
| `SMQ_AUTH_GRPC_URL` | Auth service gRPC URL | auth:7001 |
45-
| `SMQ_AUTH_GRPC_TIMEOUT` | Auth service gRPC request timeout | 300s |
46-
| `SMQ_AUTH_GRPC_CLIENT_CERT` | Auth gRPC client certificate | "" |
47-
| `SMQ_AUTH_GRPC_CLIENT_KEY` | Auth gRPC client key | "" |
48-
| `SMQ_AUTH_GRPC_SERVER_CA_CERTS` | Auth gRPC trusted CA bundle | "" |
11+
| Variable | Description | Default |
12+
| ------------------------------------- | ---------------------------------------------------- | ------------------------------ |
13+
| `SMQ_HTTP_ADAPTER_LOG_LEVEL` | Log level (debug, info, warn, error) | debug |
14+
| `SMQ_HTTP_ADAPTER_HOST` | HTTP Adapter host | http-adapter |
15+
| `SMQ_HTTP_ADAPTER_PORT` | HTTP Adapter port | 8008 |
16+
| `SMQ_HTTP_ADAPTER_SERVER_CERT` | Path to PEM-encoded server certificate (enables TLS) | "" |
17+
| `SMQ_HTTP_ADAPTER_SERVER_KEY` | Path to PEM-encoded server key | "" |
18+
| `SMQ_HTTP_ADAPTER_SERVER_CA_CERTS` | Trusted CA bundle for HTTPS server | "" |
19+
| `SMQ_HTTP_ADAPTER_CLIENT_CA_CERTS` | Client CA bundle to require mTLS on HTTPS server | "" |
20+
| `SMQ_HTTP_ADAPTER_CACHE_NUM_COUNTERS` | Cache counters for topic parsing | 200000 |
21+
| `SMQ_HTTP_ADAPTER_CACHE_MAX_COST` | Maximum cache size (bytes) | 1048576 |
22+
| `SMQ_HTTP_ADAPTER_CACHE_BUFFER_ITEMS` | Cache buffer items | 64 |
23+
| `SMQ_MESSAGE_BROKER_URL` | Message broker URL (publishing target) | nats://nats:4222 |
24+
| `SMQ_ES_URL` | Event store URL (publishing middleware) | nats://nats:4222 |
25+
| `SMQ_JAEGER_URL` | Jaeger tracing endpoint | <http://jaeger:4318/v1/traces> |
26+
| `SMQ_JAEGER_TRACE_RATIO` | Trace sampling ratio | 1.0 |
27+
| `SMQ_SEND_TELEMETRY` | Send telemetry to SuperMQ call-home server | true |
28+
| `SMQ_HTTP_ADAPTER_INSTANCE_ID` | Service instance ID (auto-generated when empty) | "" |
29+
| `SMQ_CLIENTS_GRPC_URL` | Clients service gRPC URL | clients:7006 |
30+
| `SMQ_CLIENTS_GRPC_TIMEOUT` | Clients gRPC request timeout | 300s |
31+
| `SMQ_CLIENTS_GRPC_CLIENT_CERT` | Clients gRPC client certificate | "" |
32+
| `SMQ_CLIENTS_GRPC_CLIENT_KEY` | Clients gRPC client key | "" |
33+
| `SMQ_CLIENTS_GRPC_SERVER_CA_CERTS` | Clients gRPC trusted CA bundle | "" |
34+
| `SMQ_CHANNELS_GRPC_URL` | Channels service gRPC URL | channels:7005 |
35+
| `SMQ_CHANNELS_GRPC_TIMEOUT` | Channels gRPC request timeout | 300s |
36+
| `SMQ_CHANNELS_GRPC_CLIENT_CERT` | Channels gRPC client certificate | "" |
37+
| `SMQ_CHANNELS_GRPC_CLIENT_KEY` | Channels gRPC client key | "" |
38+
| `SMQ_CHANNELS_GRPC_SERVER_CA_CERTS` | Channels gRPC trusted CA bundle | "" |
39+
| `SMQ_DOMAINS_GRPC_URL` | Domains service gRPC URL | domains:7003 |
40+
| `SMQ_DOMAINS_GRPC_TIMEOUT` | Domains gRPC request timeout | 300s |
41+
| `SMQ_DOMAINS_GRPC_CLIENT_CERT` | Domains gRPC client certificate | "" |
42+
| `SMQ_DOMAINS_GRPC_CLIENT_KEY` | Domains gRPC client key | "" |
43+
| `SMQ_DOMAINS_GRPC_SERVER_CA_CERTS` | Domains gRPC trusted CA bundle | "" |
44+
| `SMQ_AUTH_GRPC_URL` | Auth service gRPC URL | auth:7001 |
45+
| `SMQ_AUTH_GRPC_TIMEOUT` | Auth service gRPC request timeout | 300s |
46+
| `SMQ_AUTH_GRPC_CLIENT_CERT` | Auth gRPC client certificate | "" |
47+
| `SMQ_AUTH_GRPC_CLIENT_KEY` | Auth gRPC client key | "" |
48+
| `SMQ_AUTH_GRPC_SERVER_CA_CERTS` | Auth gRPC trusted CA bundle | "" |
4949

5050
## Deployment
5151

http/adapter.go

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
grpcClientsV1 "github.com/absmach/supermq/api/grpc/clients/v1"
1212
apiutil "github.com/absmach/supermq/api/http/util"
1313
smqauthn "github.com/absmach/supermq/pkg/authn"
14-
apiutil "github.com/absmach/supermq/api/http/util"
1514
"github.com/absmach/supermq/pkg/connections"
1615
"github.com/absmach/supermq/pkg/errors"
1716
svcerr "github.com/absmach/supermq/pkg/errors/service"
@@ -48,7 +47,7 @@ type adapterService struct {
4847
pubsub messaging.PubSub
4948
}
5049

51-
// NewService instantiates the http adapter service implementation.
50+
// NewService instantiates the HTTP adapter implementation.
5251
func NewService(clients grpcClientsV1.ClientsServiceClient, channels grpcChannelsV1.ChannelsServiceClient, authn smqauthn.Authentication, pubsub messaging.PubSub) Service {
5352
return &adapterService{
5453
clients: clients,
@@ -129,15 +128,10 @@ func (svc *adapterService) authorize(ctx context.Context, username, password, do
129128
if err != nil {
130129
return "", errors.Wrap(svcerr.ErrAuthentication, err)
131130
}
132-
<<<<<<< HEAD
133131

134132
// Health check topics do not require channel authorization.
135133
if topicType == messaging.HealthType {
136134
return id, nil
137-
=======
138-
if !authnRes.GetAuthenticated() {
139-
return "", svcerr.ErrAuthentication
140-
>>>>>>> 961a21b72 (feat: add websocket support to http adaper)
141135
}
142136

143137
authzReq := &grpcChannelsV1.AuthzReq{
@@ -180,12 +174,3 @@ func (svc *adapterService) authenticate(ctx context.Context, authType, token str
180174
return "", errInvalidClientType
181175
}
182176
}
183-
184-
// extractClientSecret returns value of the client secret. If there is no client key - an empty value is returned.
185-
func extractClientSecret(token string) string {
186-
if !strings.HasPrefix(token, apiutil.ClientPrefix) {
187-
return ""
188-
}
189-
190-
return strings.TrimPrefix(token, apiutil.ClientPrefix)
191-
}

http/adapter_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,10 @@ import (
1616
apiutil "github.com/absmach/supermq/api/http/util"
1717
chmocks "github.com/absmach/supermq/channels/mocks"
1818
climocks "github.com/absmach/supermq/clients/mocks"
19-
<<<<<<< HEAD
19+
smqhttp "github.com/absmach/supermq/http"
20+
"github.com/absmach/supermq/internal/testsutil"
2021
smqauthn "github.com/absmach/supermq/pkg/authn"
2122
authnmocks "github.com/absmach/supermq/pkg/authn/mocks"
22-
=======
23-
smqhttp "github.com/absmach/supermq/http"
24-
>>>>>>> 67050ee81 (ci: fix linter warnings, update tests)
2523
"github.com/absmach/supermq/pkg/connections"
2624
"github.com/absmach/supermq/pkg/errors"
2725
svcerr "github.com/absmach/supermq/pkg/errors/service"
@@ -44,9 +42,11 @@ const (
4442
)
4543

4644
var (
45+
domainID = testsutil.GenerateUUID(&testing.T{})
46+
clientID = testsutil.GenerateUUID(&testing.T{})
4747
userID = testsutil.GenerateUUID(&testing.T{})
4848
chanID = testsutil.GenerateUUID(&testing.T{})
49-
msg = messaging.Message{
49+
msg = messaging.Message{
5050
Channel: chanID,
5151
Domain: domainID,
5252
Publisher: id,
@@ -149,11 +149,11 @@ func TestSubscribe(t *testing.T) {
149149
clientID: clientID,
150150
subtopic: subTopic,
151151
topicType: messaging.MessageType,
152-
subErr: ws.ErrFailedSubscription,
152+
subErr: smqhttp.ErrFailedSubscription,
153153
authNToken: smqauthn.AuthPack(smqauthn.DomainAuth, domainID, clientKey),
154154
authNRes: &grpcClientsV1.AuthnRes{Id: clientID, Authenticated: true},
155155
authZRes: &grpcChannelsV1.AuthzRes{Authorized: true},
156-
err: ws.ErrFailedSubscription,
156+
err: smqhttp.ErrFailedSubscription,
157157
},
158158
{
159159
desc: "subscribe to channel with invalid clientKey",

0 commit comments

Comments
 (0)