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
|
|
|
|
|
|
|
|
package users_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/mainflux/mainflux"
|
|
|
|
"github.com/mainflux/mainflux/things"
|
|
|
|
"github.com/mainflux/mainflux/things/users"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
email = "john.doe@example.com"
|
|
|
|
token = "token"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestIdentify(t *testing.T) {
|
|
|
|
svc := users.NewSingleUserService(email, token)
|
|
|
|
|
|
|
|
cases := map[string]struct {
|
|
|
|
token string
|
|
|
|
id string
|
|
|
|
err error
|
|
|
|
}{
|
|
|
|
"identify non-existing user": {
|
|
|
|
token: "non-existing",
|
|
|
|
id: "",
|
|
|
|
err: things.ErrUnauthorizedAccess,
|
|
|
|
},
|
|
|
|
"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})
|
2020-10-27 19:42:53 +01:00
|
|
|
assert.Equal(t, tc.id, id.GetEmail(), fmt.Sprintf("%s: expected %s, got %s", desc, tc.id, id.GetEmail()))
|
2019-05-17 16:06:21 +02:00
|
|
|
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected %s, got %s", desc, tc.err, err))
|
|
|
|
}
|
|
|
|
}
|
2019-12-16 16:22:09 +01:00
|
|
|
|
|
|
|
func TestIssue(t *testing.T) {
|
|
|
|
svc := users.NewSingleUserService(email, token)
|
|
|
|
|
|
|
|
cases := map[string]struct {
|
|
|
|
token string
|
|
|
|
id string
|
|
|
|
err error
|
|
|
|
}{
|
|
|
|
"issue key unauthorized": {
|
|
|
|
token: "non-existing",
|
|
|
|
id: "",
|
|
|
|
err: things.ErrUnauthorizedAccess,
|
|
|
|
},
|
|
|
|
"issue key": {
|
|
|
|
token: token,
|
|
|
|
id: token,
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for desc, 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})
|
2019-12-16 16:22:09 +01:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|