This repository has been archived by the owner on Mar 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathuser_test.go
72 lines (57 loc) · 2.61 KB
/
user_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright 2020 Paul Greenberg [email protected]
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package identity
import (
"github.com/greenpau/go-identity/pkg/requests"
"testing"
)
func TestNewUser(t *testing.T) {
user := NewUser("jsmith")
if err := user.Valid(); err == nil {
t.Fatalf("user has no password, but was found to be valid")
}
if err := user.AddPassword("jsmith123", 0); err != nil {
t.Fatalf("error adding password: %s", err)
}
if err := user.Valid(); err != nil {
t.Fatalf("updated user, but was found to be invalid: %s", err)
}
roleName := "superadmin"
if err := user.AddRole(roleName); err != nil {
t.Fatalf("error adding role: %s", err)
}
if exists := user.HasRoles(); !exists {
t.Fatalf("added role, but the user has no roles")
}
if hasRole := user.HasRole(roleName); !hasRole {
t.Fatalf("added %s role, but the user has no %s role", roleName, roleName)
}
pkr := requests.NewRequest()
pkr.Key.Usage = "ssh"
pkr.Key.Payload = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDnnPNq40sWtv6WsG4cICs3Cb0C9EIeKbccOTVFk4/Ptl5oEMWHaH/e5OYAfhPsr66jC3SxCynlViXvc5+r7r9Tj1M7WGSIomZQr7c2gwyPRwT+/UBEtPi63LIAubb9rkdnZEmxU3hxdnMzeBovFLuEMZpmL5sce+sZNMMBybGP9UCRZaJhyYxy0jEJI9hlg4jRK30vkzPzsO+DNIqdAv3PNkhUqJABeMiXnxCCSQcb5S65zJPkGCRXKOlloFNt3ps9auWpJQgprrcxXFlSCvxu2UcdjegrmOMhohrxMl/VyMpbDWcyHolvZSko8uzM/G0UoR9UMrJN/AyXdzDrciqBG9EUT9NGPoA5sqWT6lt0cS7tG7tbAfV5XoN7QikwsWkDyaPfqV9EbLOkxBZCE9RdQTVtfX9MX7rBYz2MTItZ9WLIMmsPWe4RS31JhYhSiJqgGq0K8mHors5dfgMtiVaLUXG7hUpLRZ2qn29SkI0xIRiYUqLP1pV65EbJhy+1+2Vm2AgvdQrWrSofj6Dsw8IiyDtKx7ahgKnUbV3d4rtuo1hCikXu8rTlfUEXgR7kSdxaSb5uzqDLlKSe27szZxUvwsyGbTgQfLukyQZB9Kvxq6J70XMRG7UikKvyR0m/Eetp4B8RX5gvNqpd+SOl+dm+cGuqWT7UROygyf28cCqLzQ== [email protected]"
pkr.Key.Comment = "[email protected]"
if err := user.AddPublicKey(pkr); err != nil {
t.Fatalf("error adding ssh key: %s", err)
}
keyID := user.PublicKeys[0].ID
// t.Logf("key id: %s", keyID)
pkr = requests.NewRequest()
pkr.Key.ID = keyID
if err := user.DeletePublicKey(pkr); err != nil {
t.Fatalf("error deleting ssh key: %s", err)
}
if len(user.PublicKeys) > 0 {
t.Fatalf("expected 0 public keys")
}
}