2021-05-09 11:12:05 -04:00
|
|
|
package headscale
|
|
|
|
|
|
|
|
import (
|
2022-09-02 09:13:07 +02:00
|
|
|
"net/netip"
|
2022-02-22 12:45:50 +01:00
|
|
|
"testing"
|
|
|
|
|
2021-05-09 11:12:05 -04:00
|
|
|
"gopkg.in/check.v1"
|
2021-11-13 14:01:05 -05:00
|
|
|
"gorm.io/gorm"
|
2021-05-09 11:12:05 -04:00
|
|
|
)
|
|
|
|
|
2023-01-17 17:43:44 +01:00
|
|
|
func (s *Suite) TestCreateAndDestroyUser(c *check.C) {
|
|
|
|
user, err := app.CreateUser("test")
|
2021-05-09 11:12:05 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
2023-01-17 17:43:44 +01:00
|
|
|
c.Assert(user.Name, check.Equals, "test")
|
2021-05-09 11:12:05 -04:00
|
|
|
|
2023-01-17 17:43:44 +01:00
|
|
|
users, err := app.ListUsers()
|
2021-05-09 11:12:05 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
2023-01-17 17:43:44 +01:00
|
|
|
c.Assert(len(users), check.Equals, 1)
|
2021-05-09 11:12:05 -04:00
|
|
|
|
2023-01-17 17:43:44 +01:00
|
|
|
err = app.DestroyUser("test")
|
2021-05-09 11:12:05 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-01-17 17:43:44 +01:00
|
|
|
_, err = app.GetUser("test")
|
2021-05-09 11:12:05 -04:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
}
|
|
|
|
|
2023-01-17 17:43:44 +01:00
|
|
|
func (s *Suite) TestDestroyUserErrors(c *check.C) {
|
|
|
|
err := app.DestroyUser("test")
|
|
|
|
c.Assert(err, check.Equals, ErrUserNotFound)
|
2021-05-09 11:12:05 -04:00
|
|
|
|
2023-01-17 17:43:44 +01:00
|
|
|
user, err := app.CreateUser("test")
|
2021-05-09 11:12:05 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-01-17 17:43:44 +01:00
|
|
|
pak, err := app.CreatePreAuthKey(user.Name, false, false, nil, nil)
|
2021-05-09 11:12:05 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-01-17 17:43:44 +01:00
|
|
|
err = app.DestroyUser("test")
|
2021-11-13 14:01:05 -05:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-01-17 17:43:44 +01:00
|
|
|
result := app.db.Preload("User").First(&pak, "key = ?", pak.Key)
|
|
|
|
// destroying a user also deletes all associated preauthkeys
|
2021-11-13 14:01:05 -05:00
|
|
|
c.Assert(result.Error, check.Equals, gorm.ErrRecordNotFound)
|
|
|
|
|
2023-01-17 17:43:44 +01:00
|
|
|
user, err = app.CreateUser("test")
|
2021-11-13 14:01:05 -05:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-01-17 17:43:44 +01:00
|
|
|
pak, err = app.CreatePreAuthKey(user.Name, false, false, nil, nil)
|
2021-11-13 14:01:05 -05:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-15 16:16:04 +00:00
|
|
|
machine := Machine{
|
2021-05-09 11:12:05 -04:00
|
|
|
ID: 0,
|
|
|
|
MachineKey: "foo",
|
|
|
|
NodeKey: "bar",
|
|
|
|
DiscoKey: "faa",
|
2022-05-16 21:41:46 +02:00
|
|
|
Hostname: "testmachine",
|
2023-01-17 20:36:46 +01:00
|
|
|
UserID: user.ID,
|
2021-11-18 08:49:55 +00:00
|
|
|
RegisterMethod: RegisterMethodAuthKey,
|
2021-05-09 11:12:05 -04:00
|
|
|
AuthKeyID: uint(pak.ID),
|
|
|
|
}
|
2021-11-15 16:16:04 +00:00
|
|
|
app.db.Save(&machine)
|
2021-05-09 11:12:05 -04:00
|
|
|
|
2023-01-17 17:43:44 +01:00
|
|
|
err = app.DestroyUser("test")
|
|
|
|
c.Assert(err, check.Equals, ErrUserStillHasNodes)
|
2021-05-09 11:12:05 -04:00
|
|
|
}
|
2021-10-17 23:58:09 +02:00
|
|
|
|
2023-01-17 17:43:44 +01:00
|
|
|
func (s *Suite) TestRenameUser(c *check.C) {
|
|
|
|
userTest, err := app.CreateUser("test")
|
2021-10-16 11:20:06 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
2023-01-17 17:43:44 +01:00
|
|
|
c.Assert(userTest.Name, check.Equals, "test")
|
2021-10-16 11:20:06 -04:00
|
|
|
|
2023-01-17 17:43:44 +01:00
|
|
|
users, err := app.ListUsers()
|
2021-10-16 11:20:06 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
2023-01-17 17:43:44 +01:00
|
|
|
c.Assert(len(users), check.Equals, 1)
|
2021-10-16 11:20:06 -04:00
|
|
|
|
2023-01-17 17:43:44 +01:00
|
|
|
err = app.RenameUser("test", "test-renamed")
|
2021-10-16 11:20:06 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-01-17 17:43:44 +01:00
|
|
|
_, err = app.GetUser("test")
|
|
|
|
c.Assert(err, check.Equals, ErrUserNotFound)
|
2021-10-16 11:20:06 -04:00
|
|
|
|
2023-01-17 17:43:44 +01:00
|
|
|
_, err = app.GetUser("test-renamed")
|
2021-10-16 11:20:06 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-01-17 17:43:44 +01:00
|
|
|
err = app.RenameUser("test-does-not-exit", "test")
|
|
|
|
c.Assert(err, check.Equals, ErrUserNotFound)
|
2021-10-16 11:20:06 -04:00
|
|
|
|
2023-01-17 17:43:44 +01:00
|
|
|
userTest2, err := app.CreateUser("test2")
|
2021-10-16 11:20:06 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
2023-01-17 17:43:44 +01:00
|
|
|
c.Assert(userTest2.Name, check.Equals, "test2")
|
2021-10-16 11:20:06 -04:00
|
|
|
|
2023-01-17 17:43:44 +01:00
|
|
|
err = app.RenameUser("test2", "test-renamed")
|
|
|
|
c.Assert(err, check.Equals, ErrUserExists)
|
2021-10-16 11:20:06 -04:00
|
|
|
}
|
2021-10-17 23:59:44 +02:00
|
|
|
|
2021-10-17 23:58:09 +02:00
|
|
|
func (s *Suite) TestGetMapResponseUserProfiles(c *check.C) {
|
2023-01-17 17:43:44 +01:00
|
|
|
userShared1, err := app.CreateUser("shared1")
|
2021-10-17 23:58:09 +02:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-01-17 17:43:44 +01:00
|
|
|
userShared2, err := app.CreateUser("shared2")
|
2021-10-17 23:58:09 +02:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-01-17 17:43:44 +01:00
|
|
|
userShared3, err := app.CreateUser("shared3")
|
2021-10-17 23:58:09 +02:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-15 16:16:04 +00:00
|
|
|
preAuthKeyShared1, err := app.CreatePreAuthKey(
|
2023-01-17 17:43:44 +01:00
|
|
|
userShared1.Name,
|
2021-11-15 16:16:04 +00:00
|
|
|
false,
|
|
|
|
false,
|
|
|
|
nil,
|
2022-08-25 20:12:41 +10:00
|
|
|
nil,
|
2021-11-15 16:16:04 +00:00
|
|
|
)
|
2021-10-17 23:58:09 +02:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-15 16:16:04 +00:00
|
|
|
preAuthKeyShared2, err := app.CreatePreAuthKey(
|
2023-01-17 17:43:44 +01:00
|
|
|
userShared2.Name,
|
2021-11-15 16:16:04 +00:00
|
|
|
false,
|
|
|
|
false,
|
|
|
|
nil,
|
2022-08-25 20:12:41 +10:00
|
|
|
nil,
|
2021-11-15 16:16:04 +00:00
|
|
|
)
|
2021-10-17 23:58:09 +02:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-15 16:16:04 +00:00
|
|
|
preAuthKeyShared3, err := app.CreatePreAuthKey(
|
2023-01-17 17:43:44 +01:00
|
|
|
userShared3.Name,
|
2021-11-15 16:16:04 +00:00
|
|
|
false,
|
|
|
|
false,
|
|
|
|
nil,
|
2022-08-25 20:12:41 +10:00
|
|
|
nil,
|
2021-11-15 16:16:04 +00:00
|
|
|
)
|
2021-10-17 23:58:09 +02:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-15 16:16:04 +00:00
|
|
|
preAuthKey2Shared1, err := app.CreatePreAuthKey(
|
2023-01-17 17:43:44 +01:00
|
|
|
userShared1.Name,
|
2021-11-15 16:16:04 +00:00
|
|
|
false,
|
|
|
|
false,
|
|
|
|
nil,
|
2022-08-25 20:12:41 +10:00
|
|
|
nil,
|
2021-11-15 16:16:04 +00:00
|
|
|
)
|
2021-10-17 23:58:09 +02:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-01-17 17:43:44 +01:00
|
|
|
_, err = app.GetMachine(userShared1.Name, "test_get_shared_nodes_1")
|
2021-10-17 23:58:09 +02:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
2021-11-15 16:16:04 +00:00
|
|
|
machineInShared1 := &Machine{
|
2021-10-17 23:58:09 +02:00
|
|
|
ID: 1,
|
|
|
|
MachineKey: "686824e749f3b7f2a5927ee6c1e422aee5292592d9179a271ed7b3e659b44a66",
|
|
|
|
NodeKey: "686824e749f3b7f2a5927ee6c1e422aee5292592d9179a271ed7b3e659b44a66",
|
|
|
|
DiscoKey: "686824e749f3b7f2a5927ee6c1e422aee5292592d9179a271ed7b3e659b44a66",
|
2022-05-16 21:41:46 +02:00
|
|
|
Hostname: "test_get_shared_nodes_1",
|
2023-01-17 20:36:46 +01:00
|
|
|
UserID: userShared1.ID,
|
|
|
|
User: *userShared1,
|
2021-11-18 08:49:55 +00:00
|
|
|
RegisterMethod: RegisterMethodAuthKey,
|
2022-09-02 09:13:07 +02:00
|
|
|
IPAddresses: []netip.Addr{netip.MustParseAddr("100.64.0.1")},
|
2021-11-15 16:16:04 +00:00
|
|
|
AuthKeyID: uint(preAuthKeyShared1.ID),
|
2021-10-17 23:58:09 +02:00
|
|
|
}
|
2021-11-15 16:16:04 +00:00
|
|
|
app.db.Save(machineInShared1)
|
2021-10-17 23:58:09 +02:00
|
|
|
|
2023-01-17 17:43:44 +01:00
|
|
|
_, err = app.GetMachine(userShared1.Name, machineInShared1.Hostname)
|
2021-10-17 23:58:09 +02:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-15 16:16:04 +00:00
|
|
|
machineInShared2 := &Machine{
|
2021-10-17 23:58:09 +02:00
|
|
|
ID: 2,
|
|
|
|
MachineKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
|
|
|
|
NodeKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
|
|
|
|
DiscoKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
|
2022-05-16 21:41:46 +02:00
|
|
|
Hostname: "test_get_shared_nodes_2",
|
2023-01-17 20:36:46 +01:00
|
|
|
UserID: userShared2.ID,
|
|
|
|
User: *userShared2,
|
2021-11-18 08:49:55 +00:00
|
|
|
RegisterMethod: RegisterMethodAuthKey,
|
2022-09-02 09:13:07 +02:00
|
|
|
IPAddresses: []netip.Addr{netip.MustParseAddr("100.64.0.2")},
|
2021-11-15 16:16:04 +00:00
|
|
|
AuthKeyID: uint(preAuthKeyShared2.ID),
|
2021-10-17 23:58:09 +02:00
|
|
|
}
|
2021-11-15 16:16:04 +00:00
|
|
|
app.db.Save(machineInShared2)
|
2021-10-17 23:58:09 +02:00
|
|
|
|
2023-01-17 17:43:44 +01:00
|
|
|
_, err = app.GetMachine(userShared2.Name, machineInShared2.Hostname)
|
2021-10-17 23:58:09 +02:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-15 16:16:04 +00:00
|
|
|
machineInShared3 := &Machine{
|
2021-10-17 23:58:09 +02:00
|
|
|
ID: 3,
|
|
|
|
MachineKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
|
|
|
|
NodeKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
|
|
|
|
DiscoKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
|
2022-05-16 21:41:46 +02:00
|
|
|
Hostname: "test_get_shared_nodes_3",
|
2023-01-17 20:36:46 +01:00
|
|
|
UserID: userShared3.ID,
|
|
|
|
User: *userShared3,
|
2021-11-18 08:49:55 +00:00
|
|
|
RegisterMethod: RegisterMethodAuthKey,
|
2022-09-02 09:13:07 +02:00
|
|
|
IPAddresses: []netip.Addr{netip.MustParseAddr("100.64.0.3")},
|
2021-11-15 16:16:04 +00:00
|
|
|
AuthKeyID: uint(preAuthKeyShared3.ID),
|
2021-10-17 23:58:09 +02:00
|
|
|
}
|
2021-11-15 16:16:04 +00:00
|
|
|
app.db.Save(machineInShared3)
|
2021-10-17 23:58:09 +02:00
|
|
|
|
2023-01-17 17:43:44 +01:00
|
|
|
_, err = app.GetMachine(userShared3.Name, machineInShared3.Hostname)
|
2021-10-17 23:58:09 +02:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-15 16:16:04 +00:00
|
|
|
machine2InShared1 := &Machine{
|
2021-10-17 23:58:09 +02:00
|
|
|
ID: 4,
|
|
|
|
MachineKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
|
|
|
|
NodeKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
|
|
|
|
DiscoKey: "dec46ef9dc45c7d2f03bfcd5a640d9e24e3cc68ce3d9da223867c9bc6d5e9863",
|
2022-05-16 21:41:46 +02:00
|
|
|
Hostname: "test_get_shared_nodes_4",
|
2023-01-17 20:36:46 +01:00
|
|
|
UserID: userShared1.ID,
|
|
|
|
User: *userShared1,
|
2021-11-18 08:49:55 +00:00
|
|
|
RegisterMethod: RegisterMethodAuthKey,
|
2022-09-02 09:13:07 +02:00
|
|
|
IPAddresses: []netip.Addr{netip.MustParseAddr("100.64.0.4")},
|
2021-11-15 16:16:04 +00:00
|
|
|
AuthKeyID: uint(preAuthKey2Shared1.ID),
|
2021-10-17 23:58:09 +02:00
|
|
|
}
|
2021-11-15 16:16:04 +00:00
|
|
|
app.db.Save(machine2InShared1)
|
2021-10-17 23:58:09 +02:00
|
|
|
|
2021-11-15 16:16:04 +00:00
|
|
|
peersOfMachine1InShared1, err := app.getPeers(machineInShared1)
|
2021-10-17 23:58:09 +02:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2022-11-22 17:36:19 +01:00
|
|
|
userProfiles := app.getMapResponseUserProfiles(
|
2021-11-15 16:16:04 +00:00
|
|
|
*machineInShared1,
|
|
|
|
peersOfMachine1InShared1,
|
|
|
|
)
|
2021-10-17 23:58:09 +02:00
|
|
|
|
2022-02-25 10:27:27 +01:00
|
|
|
c.Assert(len(userProfiles), check.Equals, 3)
|
2021-10-17 23:58:09 +02:00
|
|
|
|
|
|
|
found := false
|
2021-11-15 16:16:04 +00:00
|
|
|
for _, userProfiles := range userProfiles {
|
2023-01-17 17:43:44 +01:00
|
|
|
if userProfiles.DisplayName == userShared1.Name {
|
2021-10-17 23:58:09 +02:00
|
|
|
found = true
|
2021-11-14 16:46:09 +01:00
|
|
|
|
2021-10-17 23:58:09 +02:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c.Assert(found, check.Equals, true)
|
|
|
|
|
|
|
|
found = false
|
2021-11-15 16:16:04 +00:00
|
|
|
for _, userProfile := range userProfiles {
|
2023-01-17 17:43:44 +01:00
|
|
|
if userProfile.DisplayName == userShared2.Name {
|
2021-10-17 23:58:09 +02:00
|
|
|
found = true
|
2021-11-14 16:46:09 +01:00
|
|
|
|
2021-10-17 23:58:09 +02:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c.Assert(found, check.Equals, true)
|
|
|
|
}
|
2022-02-22 12:45:50 +01:00
|
|
|
|
2022-03-07 22:55:54 +01:00
|
|
|
func TestNormalizeToFQDNRules(t *testing.T) {
|
2022-02-22 12:45:50 +01:00
|
|
|
type args struct {
|
2022-02-23 14:03:07 +01:00
|
|
|
name string
|
|
|
|
stripEmailDomain bool
|
2022-02-22 12:45:50 +01:00
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want string
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
2022-02-23 14:03:07 +01:00
|
|
|
name: "normalize simple name",
|
|
|
|
args: args{
|
|
|
|
name: "normalize-simple.name",
|
|
|
|
stripEmailDomain: false,
|
|
|
|
},
|
2022-02-22 12:45:50 +01:00
|
|
|
want: "normalize-simple.name",
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
2022-02-23 14:03:07 +01:00
|
|
|
name: "normalize an email",
|
|
|
|
args: args{
|
|
|
|
name: "foo.bar@example.com",
|
|
|
|
stripEmailDomain: false,
|
|
|
|
},
|
2022-02-22 12:45:50 +01:00
|
|
|
want: "foo.bar.example.com",
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
2022-02-23 14:03:07 +01:00
|
|
|
name: "normalize an email domain should be removed",
|
|
|
|
args: args{
|
|
|
|
name: "foo.bar@example.com",
|
|
|
|
stripEmailDomain: true,
|
|
|
|
},
|
|
|
|
want: "foo.bar",
|
|
|
|
wantErr: false,
|
2022-02-23 14:28:20 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "strip enabled no email passed as argument",
|
|
|
|
args: args{
|
|
|
|
name: "not-email-and-strip-enabled",
|
|
|
|
stripEmailDomain: true,
|
|
|
|
},
|
|
|
|
want: "not-email-and-strip-enabled",
|
|
|
|
wantErr: false,
|
2022-02-23 14:03:07 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "normalize complex email",
|
|
|
|
args: args{
|
|
|
|
name: "foo.bar+complex-email@example.com",
|
|
|
|
stripEmailDomain: false,
|
|
|
|
},
|
2022-02-22 12:45:50 +01:00
|
|
|
want: "foo.bar-complex-email.example.com",
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
2023-01-17 17:43:44 +01:00
|
|
|
name: "user name with space",
|
2022-02-23 14:03:07 +01:00
|
|
|
args: args{
|
|
|
|
name: "name space",
|
|
|
|
stripEmailDomain: false,
|
|
|
|
},
|
2022-02-22 12:45:50 +01:00
|
|
|
want: "name-space",
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
2023-01-17 17:43:44 +01:00
|
|
|
name: "user with quote",
|
2022-02-23 14:03:07 +01:00
|
|
|
args: args{
|
|
|
|
name: "Jamie's iPhone 5",
|
|
|
|
stripEmailDomain: false,
|
|
|
|
},
|
2022-02-22 12:45:50 +01:00
|
|
|
want: "jamies-iphone-5",
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2022-03-07 22:55:54 +01:00
|
|
|
got, err := NormalizeToFQDNRules(tt.args.name, tt.args.stripEmailDomain)
|
2022-02-22 12:45:50 +01:00
|
|
|
if (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf(
|
2022-03-07 22:55:54 +01:00
|
|
|
"NormalizeToFQDNRules() error = %v, wantErr %v",
|
2022-02-22 12:45:50 +01:00
|
|
|
err,
|
|
|
|
tt.wantErr,
|
|
|
|
)
|
2022-02-22 21:05:39 +01:00
|
|
|
|
2022-02-22 12:45:50 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if got != tt.want {
|
2022-03-07 22:55:54 +01:00
|
|
|
t.Errorf("NormalizeToFQDNRules() = %v, want %v", got, tt.want)
|
2022-02-22 12:45:50 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-02-23 11:07:24 +01:00
|
|
|
|
2022-03-07 22:55:54 +01:00
|
|
|
func TestCheckForFQDNRules(t *testing.T) {
|
2022-02-23 11:07:24 +01:00
|
|
|
type args struct {
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
2023-01-17 17:43:44 +01:00
|
|
|
name: "valid: user",
|
|
|
|
args: args{name: "valid-user"},
|
2022-02-23 11:07:24 +01:00
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
2023-01-17 17:43:44 +01:00
|
|
|
name: "invalid: capitalized user",
|
|
|
|
args: args{name: "Invalid-CapItaLIzed-user"},
|
2022-02-23 11:07:24 +01:00
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
2023-01-17 17:43:44 +01:00
|
|
|
name: "invalid: email as user",
|
2022-02-23 11:07:24 +01:00
|
|
|
args: args{name: "foo.bar@example.com"},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
2023-01-17 17:43:44 +01:00
|
|
|
name: "invalid: chars in user name",
|
|
|
|
args: args{name: "super-user+name"},
|
2022-02-23 11:07:24 +01:00
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
2023-01-17 17:43:44 +01:00
|
|
|
name: "invalid: too long name for user",
|
2022-02-23 11:07:24 +01:00
|
|
|
args: args{
|
2023-01-17 20:41:39 +01:00
|
|
|
name: "super-long-useruseruser-name-that-should-be-a-little-more-than-63-chars",
|
2022-02-23 11:07:24 +01:00
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2022-03-07 22:55:54 +01:00
|
|
|
if err := CheckForFQDNRules(tt.args.name); (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("CheckForFQDNRules() error = %v, wantErr %v", err, tt.wantErr)
|
2022-02-23 11:07:24 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-05-01 21:40:18 +04:00
|
|
|
|
2023-01-17 17:43:44 +01:00
|
|
|
func (s *Suite) TestSetMachineUser(c *check.C) {
|
|
|
|
oldUser, err := app.CreateUser("old")
|
2022-05-01 21:40:18 +04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-01-17 17:43:44 +01:00
|
|
|
newUser, err := app.CreateUser("new")
|
2022-05-01 21:40:18 +04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-01-17 17:43:44 +01:00
|
|
|
pak, err := app.CreatePreAuthKey(oldUser.Name, false, false, nil, nil)
|
2022-05-01 21:40:18 +04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
machine := Machine{
|
|
|
|
ID: 0,
|
|
|
|
MachineKey: "foo",
|
|
|
|
NodeKey: "bar",
|
|
|
|
DiscoKey: "faa",
|
2022-05-16 21:41:46 +02:00
|
|
|
Hostname: "testmachine",
|
2023-01-17 20:36:46 +01:00
|
|
|
UserID: oldUser.ID,
|
2022-05-01 21:40:18 +04:00
|
|
|
RegisterMethod: RegisterMethodAuthKey,
|
|
|
|
AuthKeyID: uint(pak.ID),
|
|
|
|
}
|
|
|
|
app.db.Save(&machine)
|
2023-01-17 17:43:44 +01:00
|
|
|
c.Assert(machine.UserID, check.Equals, oldUser.ID)
|
2022-05-01 21:40:18 +04:00
|
|
|
|
2023-01-17 17:43:44 +01:00
|
|
|
err = app.SetMachineUser(&machine, newUser.Name)
|
2022-05-01 21:40:18 +04:00
|
|
|
c.Assert(err, check.IsNil)
|
2023-01-17 17:43:44 +01:00
|
|
|
c.Assert(machine.UserID, check.Equals, newUser.ID)
|
|
|
|
c.Assert(machine.User.Name, check.Equals, newUser.Name)
|
2022-05-01 21:40:18 +04:00
|
|
|
|
2023-01-17 17:43:44 +01:00
|
|
|
err = app.SetMachineUser(&machine, "non-existing-user")
|
|
|
|
c.Assert(err, check.Equals, ErrUserNotFound)
|
2022-05-01 21:40:18 +04:00
|
|
|
|
2023-01-17 17:43:44 +01:00
|
|
|
err = app.SetMachineUser(&machine, newUser.Name)
|
2022-05-01 21:40:18 +04:00
|
|
|
c.Assert(err, check.IsNil)
|
2023-01-17 17:43:44 +01:00
|
|
|
c.Assert(machine.UserID, check.Equals, newUser.ID)
|
|
|
|
c.Assert(machine.User.Name, check.Equals, newUser.Name)
|
2022-05-01 21:40:18 +04:00
|
|
|
}
|