2019-12-15 08:11:31 +00:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-27 18:20:29 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2019-12-15 08:11:31 +00:00
|
|
|
|
2022-09-02 19:18:23 +00:00
|
|
|
package integration
|
2019-12-15 08:11:31 +00:00
|
|
|
|
|
|
|
import (
|
2024-03-27 21:02:51 +00:00
|
|
|
"errors"
|
2019-12-17 01:49:07 +00:00
|
|
|
"net/url"
|
2024-03-27 21:02:51 +00:00
|
|
|
"os/exec"
|
2019-12-15 08:11:31 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2020-09-29 01:16:52 +00:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2019-12-15 08:11:31 +00:00
|
|
|
|
2023-07-21 09:28:19 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2019-12-15 08:11:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func Test_CmdKeys(t *testing.T) {
|
2019-12-17 01:49:07 +00:00
|
|
|
onGiteaRun(t, func(*testing.T, *url.URL) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args []string
|
|
|
|
wantErr bool
|
|
|
|
expectedOutput string
|
|
|
|
}{
|
2024-03-27 21:02:51 +00:00
|
|
|
{"test_empty_1", []string{"--username=git", "--type=test", "--content=test"}, true, ""},
|
|
|
|
{"test_empty_2", []string{"-e", "git", "-u", "git", "-t", "test", "-k", "test"}, true, ""},
|
2022-01-20 17:46:10 +00:00
|
|
|
{
|
|
|
|
"with_key",
|
2024-03-27 21:02:51 +00:00
|
|
|
[]string{"-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="},
|
2019-12-17 01:49:07 +00:00
|
|
|
false,
|
2021-11-23 08:12:02 +00:00
|
|
|
"# gitea public key\ncommand=\"" + setting.AppPath + " --config=" + util.ShellEscape(setting.CustomConf) + " serv key-1\",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty,no-user-rc,restrict ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDWVj0fQ5N8wNc0LVNA41wDLYJ89ZIbejrPfg/avyj3u/ZohAKsQclxG4Ju0VirduBFF9EOiuxoiFBRr3xRpqzpsZtnMPkWVWb+akZwBFAx8p+jKdy4QXR/SZqbVobrGwip2UjSrri1CtBxpJikojRIZfCnDaMOyd9Jp6KkujvniFzUWdLmCPxUE9zhTaPu0JsEP7MW0m6yx7ZUhHyfss+NtqmFTaDO+QlMR7L2QkDliN2Jl3Xa3PhuWnKJfWhdAq1Cw4oraKUOmIgXLkuiuxVQ6mD3AiFupkmfqdHq6h+uHHmyQqv3gU+/sD8GbGAhf6ftqhTsXjnv1Aj4R8NoDf9BS6KRkzkeun5UisSzgtfQzjOMEiJtmrep2ZQrMGahrXa+q4VKr0aKJfm+KlLfwm/JztfsBcqQWNcTURiCFqz+fgZw0Ey/de0eyMzldYTdXXNRYCKjs9bvBK+6SSXRM7AhftfQ0ZuoW5+gtinPrnmoOaSCEJbAiEiTO/BzOHgowiM= user2@localhost\n",
|
2019-12-17 01:49:07 +00:00
|
|
|
},
|
2024-03-27 21:02:51 +00:00
|
|
|
{"invalid", []string{"--not-a-flag=git"}, true, "Incorrect Usage: flag provided but not defined: -not-a-flag\n\n"},
|
2019-12-17 01:49:07 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2024-03-27 21:02:51 +00:00
|
|
|
out, err := runMainApp("keys", tt.args...)
|
|
|
|
|
|
|
|
var exitErr *exec.ExitError
|
|
|
|
if errors.As(err, &exitErr) {
|
|
|
|
t.Log(string(exitErr.Stderr))
|
|
|
|
}
|
2023-07-21 09:28:19 +00:00
|
|
|
if tt.wantErr {
|
|
|
|
assert.Error(t, err)
|
|
|
|
} else {
|
|
|
|
assert.NoError(t, err)
|
2019-12-17 01:49:07 +00:00
|
|
|
}
|
2024-03-27 21:02:51 +00:00
|
|
|
assert.Equal(t, tt.expectedOutput, out)
|
2019-12-17 01:49:07 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
2019-12-15 08:11:31 +00:00
|
|
|
}
|