|
| 1 | +package shell |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/cheekybits/is" |
| 8 | +) |
| 9 | + |
| 10 | +func TestKeyGen(t *testing.T) { |
| 11 | + is := is.New(t) |
| 12 | + s := NewShell(shellUrl) |
| 13 | + |
| 14 | + defer func() { |
| 15 | + _, err := s.KeyRm(context.Background(), "testKey1") |
| 16 | + is.Nil(err) |
| 17 | + }() |
| 18 | + key1, err := s.KeyGen(context.Background(), "testKey1", KeyGen.Type("ed25519")) |
| 19 | + is.Nil(err) |
| 20 | + is.Equal(key1.Name, "testKey1") |
| 21 | + is.NotNil(key1.Id) |
| 22 | + |
| 23 | + defer func() { |
| 24 | + _, err = s.KeyRm(context.Background(), "testKey2") |
| 25 | + is.Nil(err) |
| 26 | + }() |
| 27 | + key2, err := s.KeyGen(context.Background(), "testKey2", KeyGen.Type("ed25519")) |
| 28 | + is.Nil(err) |
| 29 | + is.Equal(key2.Name, "testKey2") |
| 30 | + is.NotNil(key2.Id) |
| 31 | + |
| 32 | + defer func() { |
| 33 | + _, err = s.KeyRm(context.Background(), "testKey3") |
| 34 | + is.Nil(err) |
| 35 | + }() |
| 36 | + key3, err := s.KeyGen(context.Background(), "testKey3", KeyGen.Type("rsa")) |
| 37 | + is.Nil(err) |
| 38 | + is.Equal(key3.Name, "testKey3") |
| 39 | + is.NotNil(key3.Id) |
| 40 | + |
| 41 | + defer func() { |
| 42 | + _, err = s.KeyRm(context.Background(), "testKey4") |
| 43 | + is.Nil(err) |
| 44 | + }() |
| 45 | + key4, err := s.KeyGen(context.Background(), "testKey4", KeyGen.Type("rsa"), KeyGen.Size(4096)) |
| 46 | + is.Nil(err) |
| 47 | + is.Equal(key4.Name, "testKey4") |
| 48 | + is.NotNil(key4.Id) |
| 49 | + |
| 50 | + _, err = s.KeyGen(context.Background(), "testKey5", KeyGen.Type("rsa"), KeyGen.Size(1024)) |
| 51 | + is.NotNil(err) |
| 52 | + is.Equal(err.Error(), "key/gen: rsa keys must be >= 2048 bits to be useful") |
| 53 | +} |
| 54 | + |
| 55 | +func TestKeyList(t *testing.T) { |
| 56 | + is := is.New(t) |
| 57 | + s := NewShell(shellUrl) |
| 58 | + |
| 59 | + defer func() { |
| 60 | + _, err := s.KeyRm(context.Background(), "testKey") |
| 61 | + is.Nil(err) |
| 62 | + }() |
| 63 | + key, err := s.KeyGen(context.Background(), "testKey") |
| 64 | + is.Nil(err) |
| 65 | + |
| 66 | + keys, err := s.KeyList(context.Background()) |
| 67 | + is.Nil(err) |
| 68 | + |
| 69 | + is.Equal(len(keys), 2) |
| 70 | + is.Equal(keys[0].Name, "self") |
| 71 | + is.NotNil(keys[0].Id) |
| 72 | + is.NotNil(keys[1].Id, key.Id) |
| 73 | + is.NotNil(keys[1].Name, key.Name) |
| 74 | +} |
| 75 | + |
| 76 | +func TestKeyRename(t *testing.T) { |
| 77 | + is := is.New(t) |
| 78 | + s := NewShell(shellUrl) |
| 79 | + |
| 80 | + key, err := s.KeyGen(context.Background(), "test1") |
| 81 | + is.Nil(err) |
| 82 | + |
| 83 | + out, err := s.KeyRename(context.Background(), "test1", "test2", false) |
| 84 | + is.Nil(err) |
| 85 | + |
| 86 | + is.Equal(out.Now, "test2") |
| 87 | + is.Equal(out.Was, "test1") |
| 88 | + is.Equal(out.Id, key.Id) |
| 89 | + is.False(out.Overwrite) |
| 90 | + |
| 91 | + _, err = s.KeyRm(context.Background(), "test2") |
| 92 | + is.Nil(err) |
| 93 | + |
| 94 | + _, err = s.KeyRename(context.Background(), "test2", "test1", false) |
| 95 | + is.NotNil(err) |
| 96 | + is.Equal(err.Error(), "key/rename: no key named test2 was found") |
| 97 | +} |
| 98 | + |
| 99 | +func TestKeyRm(t *testing.T) { |
| 100 | + is := is.New(t) |
| 101 | + s := NewShell(shellUrl) |
| 102 | + |
| 103 | + key, err := s.KeyGen(context.Background(), "testKey") |
| 104 | + is.Nil(err) |
| 105 | + |
| 106 | + keys, err := s.KeyRm(context.Background(), "testKey") |
| 107 | + is.Nil(err) |
| 108 | + |
| 109 | + is.Equal(len(keys), 1) |
| 110 | + is.Equal(keys[0].Name, key.Name) |
| 111 | + is.Equal(keys[0].Id, key.Id) |
| 112 | + |
| 113 | + _, err = s.KeyRm(context.Background(), "testKey") |
| 114 | + is.NotNil(err) |
| 115 | + is.Equal(err.Error(), "key/rm: no key named testKey was found") |
| 116 | +} |
0 commit comments