2019-10-07 08:14:47 -06:00
|
|
|
// Copyright (c) Mainflux
|
2019-05-17 16:06:21 +02:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2021-10-11 16:08:26 +02:00
|
|
|
package standalone_test
|
2019-05-17 16:06:21 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/mainflux/mainflux"
|
2022-01-27 17:03:57 +01:00
|
|
|
"github.com/mainflux/mainflux/pkg/errors"
|
2021-10-11 16:08:26 +02:00
|
|
|
"github.com/mainflux/mainflux/things/standalone"
|
2019-05-17 16:06:21 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
email = "john.doe@example.com"
|
|
|
|
token = "token"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestIdentify(t *testing.T) {
|
2021-10-11 16:08:26 +02:00
|
|
|
svc := standalone.NewAuthService(email, token)
|
2019-05-17 16:06:21 +02:00
|
|
|
|
2023-04-12 08:30:01 -07:00
|
|
|
cases := []struct {
|
|
|
|
desc string
|
2019-05-17 16:06:21 +02:00
|
|
|
token string
|
|
|
|
id string
|
|
|
|
err error
|
|
|
|
}{
|
2023-04-12 08:30:01 -07:00
|
|
|
{
|
|
|
|
desc: "identify non-existing user",
|
2019-05-17 16:06:21 +02:00
|
|
|
token: "non-existing",
|
|
|
|
id: "",
|
2022-02-01 17:33:23 +01:00
|
|
|
err: errors.ErrAuthentication,
|
2019-05-17 16:06:21 +02:00
|
|
|
},
|
2023-04-12 08:30:01 -07:00
|
|
|
{
|
|
|
|
desc: "identify existing user",
|
2019-05-17 16:06:21 +02:00
|
|
|
token: token,
|
|
|
|
id: email,
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-04-12 08:30:01 -07:00
|
|
|
for _, tc := range cases {
|
2019-05-17 16:06:21 +02:00
|
|
|
id, err := svc.Identify(context.Background(), &mainflux.Token{Value: tc.token})
|
2023-04-12 08:30:01 -07:00
|
|
|
assert.Equal(t, tc.id, id.GetEmail(), fmt.Sprintf("%s: expected %s, got %s", tc.desc, tc.id, id.GetEmail()))
|
|
|
|
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected %s, got %s", tc.desc, tc.err, err))
|
2019-05-17 16:06:21 +02:00
|
|
|
}
|
|
|
|
}
|
2019-12-16 16:22:09 +01:00
|
|
|
|
|
|
|
func TestIssue(t *testing.T) {
|
2021-10-11 16:08:26 +02:00
|
|
|
svc := standalone.NewAuthService(email, token)
|
2019-12-16 16:22:09 +01:00
|
|
|
|
2023-04-12 08:30:01 -07:00
|
|
|
cases := []struct {
|
|
|
|
desc string
|
2019-12-16 16:22:09 +01:00
|
|
|
token string
|
|
|
|
id string
|
|
|
|
err error
|
|
|
|
}{
|
2023-04-12 08:30:01 -07:00
|
|
|
{
|
|
|
|
desc: "issue key",
|
2019-12-16 16:22:09 +01:00
|
|
|
token: token,
|
|
|
|
id: token,
|
|
|
|
err: nil,
|
|
|
|
},
|
2023-04-12 08:30:01 -07:00
|
|
|
{
|
|
|
|
desc: "issue key with an invalid token",
|
|
|
|
token: "non-existing",
|
|
|
|
id: "",
|
|
|
|
err: errors.ErrAuthentication,
|
|
|
|
},
|
2019-12-16 16:22:09 +01:00
|
|
|
}
|
|
|
|
|
2023-04-12 08:30:01 -07:00
|
|
|
for _, tc := range cases {
|
2020-10-27 19:42:53 +01:00
|
|
|
id, err := svc.Issue(context.Background(), &mainflux.IssueReq{Id: tc.id, Email: tc.token, Type: 0})
|
2023-04-12 08:30:01 -07:00
|
|
|
assert.Equal(t, tc.id, id.GetValue(), fmt.Sprintf("%s: expected %s, got %s", tc.desc, tc.id, id.GetValue()))
|
|
|
|
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected %s, got %s", tc.desc, tc.err, err))
|
2019-12-16 16:22:09 +01:00
|
|
|
}
|
|
|
|
}
|