mirror of
https://github.com/mainflux/mainflux.git
synced 2025-05-01 13:48:56 +08:00

* MF-1261 - Use StatusUnauthorized for authn and StatusForbidden for authz Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * ErrExternalKey typo Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Rename ErrUnauthorizedAcces -> ErrAuthentication Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix bootstrap error Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix status code in openapi Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix test description Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix test description Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix test description Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Add errors cases Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix status codes Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Add gRPC stutus code Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix tests description Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix openapi and encodeError Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix grpc message Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix test descriptions Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Revert sdk error Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix typo Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>
75 lines
1.7 KiB
Go
75 lines
1.7 KiB
Go
// Copyright (c) Mainflux
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package standalone_test
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/mainflux/mainflux"
|
|
"github.com/mainflux/mainflux/pkg/errors"
|
|
"github.com/mainflux/mainflux/things/standalone"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
const (
|
|
email = "john.doe@example.com"
|
|
token = "token"
|
|
)
|
|
|
|
func TestIdentify(t *testing.T) {
|
|
svc := standalone.NewAuthService(email, token)
|
|
|
|
cases := map[string]struct {
|
|
token string
|
|
id string
|
|
err error
|
|
}{
|
|
"identify non-existing user": {
|
|
token: "non-existing",
|
|
id: "",
|
|
err: errors.ErrAuthentication,
|
|
},
|
|
"identify existing user": {
|
|
token: token,
|
|
id: email,
|
|
err: nil,
|
|
},
|
|
}
|
|
|
|
for desc, tc := range cases {
|
|
id, err := svc.Identify(context.Background(), &mainflux.Token{Value: tc.token})
|
|
assert.Equal(t, tc.id, id.GetEmail(), fmt.Sprintf("%s: expected %s, got %s", desc, tc.id, id.GetEmail()))
|
|
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected %s, got %s", desc, tc.err, err))
|
|
}
|
|
}
|
|
|
|
func TestIssue(t *testing.T) {
|
|
svc := standalone.NewAuthService(email, token)
|
|
|
|
cases := map[string]struct {
|
|
token string
|
|
id string
|
|
err error
|
|
}{
|
|
"issue key with an invalid token": {
|
|
token: "non-existing",
|
|
id: "",
|
|
err: errors.ErrAuthentication,
|
|
},
|
|
"issue key": {
|
|
token: token,
|
|
id: token,
|
|
err: nil,
|
|
},
|
|
}
|
|
|
|
for desc, tc := range cases {
|
|
id, err := svc.Issue(context.Background(), &mainflux.IssueReq{Id: tc.id, Email: tc.token, Type: 0})
|
|
assert.Equal(t, tc.id, id.GetValue(), fmt.Sprintf("%s: expected %s, got %s", desc, tc.id, id.GetValue()))
|
|
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected %s, got %s", desc, tc.err, err))
|
|
}
|
|
}
|