|
| 1 | +// Copyright 2019 The Gitea Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a MIT-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package integrations |
| 6 | + |
| 7 | +import ( |
| 8 | + "bytes" |
| 9 | + "flag" |
| 10 | + "io" |
| 11 | + "os" |
| 12 | + "testing" |
| 13 | + |
| 14 | + "code.gitea.io/gitea/cmd" |
| 15 | + "code.gitea.io/gitea/modules/setting" |
| 16 | + |
| 17 | + "github.com/urfave/cli" |
| 18 | +) |
| 19 | + |
| 20 | +func Test_CmdKeys(t *testing.T) { |
| 21 | + defer prepareTestEnv(t)() |
| 22 | + |
| 23 | + tests := []struct { |
| 24 | + name string |
| 25 | + args []string |
| 26 | + wantErr bool |
| 27 | + expectedOutput string |
| 28 | + }{ |
| 29 | + {"test_empty_1", []string{"keys", "--username=git", "--type=test", "--content=test"}, true, ""}, |
| 30 | + {"test_empty_2", []string{"keys", "-e", "git", "-u", "git", "-t", "test", "-k", "test"}, true, ""}, |
| 31 | + {"with_key", |
| 32 | + []string{"keys", "-e", "git", "-u", "git", "-t", "ssh-rsa", "-k", "AAAAB3NzaC1yc2EAAAADAQABAAABgQDWVj0fQ5N8wNc0LVNA41wDLYJ89ZIbejrPfg/avyj3u/ZohAKsQclxG4Ju0VirduBFF9EOiuxoiFBRr3xRpqzpsZtnMPkWVWb+akZwBFAx8p+jKdy4QXR/SZqbVobrGwip2UjSrri1CtBxpJikojRIZfCnDaMOyd9Jp6KkujvniFzUWdLmCPxUE9zhTaPu0JsEP7MW0m6yx7ZUhHyfss+NtqmFTaDO+QlMR7L2QkDliN2Jl3Xa3PhuWnKJfWhdAq1Cw4oraKUOmIgXLkuiuxVQ6mD3AiFupkmfqdHq6h+uHHmyQqv3gU+/sD8GbGAhf6ftqhTsXjnv1Aj4R8NoDf9BS6KRkzkeun5UisSzgtfQzjOMEiJtmrep2ZQrMGahrXa+q4VKr0aKJfm+KlLfwm/JztfsBcqQWNcTURiCFqz+fgZw0Ey/de0eyMzldYTdXXNRYCKjs9bvBK+6SSXRM7AhftfQ0ZuoW5+gtinPrnmoOaSCEJbAiEiTO/BzOHgowiM="}, |
| 33 | + false, |
| 34 | + "# gitea public key\ncommand=\"" + setting.AppPath + " --config='" + setting.CustomConf + "' serv key-1\",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDWVj0fQ5N8wNc0LVNA41wDLYJ89ZIbejrPfg/avyj3u/ZohAKsQclxG4Ju0VirduBFF9EOiuxoiFBRr3xRpqzpsZtnMPkWVWb+akZwBFAx8p+jKdy4QXR/SZqbVobrGwip2UjSrri1CtBxpJikojRIZfCnDaMOyd9Jp6KkujvniFzUWdLmCPxUE9zhTaPu0JsEP7MW0m6yx7ZUhHyfss+NtqmFTaDO+QlMR7L2QkDliN2Jl3Xa3PhuWnKJfWhdAq1Cw4oraKUOmIgXLkuiuxVQ6mD3AiFupkmfqdHq6h+uHHmyQqv3gU+/sD8GbGAhf6ftqhTsXjnv1Aj4R8NoDf9BS6KRkzkeun5UisSzgtfQzjOMEiJtmrep2ZQrMGahrXa+q4VKr0aKJfm+KlLfwm/JztfsBcqQWNcTURiCFqz+fgZw0Ey/de0eyMzldYTdXXNRYCKjs9bvBK+6SSXRM7AhftfQ0ZuoW5+gtinPrnmoOaSCEJbAiEiTO/BzOHgowiM= user2@localhost\n\n", |
| 35 | + }, |
| 36 | + {"invalid", []string{"keys", "--not-a-flag=git"}, true, "Incorrect Usage: flag provided but not defined: -not-a-flag\n\n"}, |
| 37 | + } |
| 38 | + for _, tt := range tests { |
| 39 | + t.Run(tt.name, func(t *testing.T) { |
| 40 | + realStdout := os.Stdout //Backup Stdout |
| 41 | + r, w, _ := os.Pipe() |
| 42 | + os.Stdout = w |
| 43 | + |
| 44 | + set := flag.NewFlagSet("keys", 0) |
| 45 | + _ = set.Parse(tt.args) |
| 46 | + context := cli.NewContext(&cli.App{Writer: os.Stdout}, set, nil) |
| 47 | + err := cmd.CmdKeys.Run(context) |
| 48 | + if (err != nil) != tt.wantErr { |
| 49 | + t.Errorf("CmdKeys.Run() error = %v, wantErr %v", err, tt.wantErr) |
| 50 | + } |
| 51 | + w.Close() |
| 52 | + var buf bytes.Buffer |
| 53 | + io.Copy(&buf, r) |
| 54 | + commandOutput := buf.String() |
| 55 | + if tt.expectedOutput != commandOutput { |
| 56 | + t.Errorf("expectedOutput: %#v, commandOutput: %#v", tt.expectedOutput, commandOutput) |
| 57 | + } |
| 58 | + //Restore stdout |
| 59 | + os.Stdout = realStdout |
| 60 | + }) |
| 61 | + } |
| 62 | +} |
0 commit comments