You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
3.0 KiB

  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. package integrations
  5. import (
  6. "bytes"
  7. "flag"
  8. "io"
  9. "net/url"
  10. "os"
  11. "strconv"
  12. "testing"
  13. "code.gitea.io/gitea/cmd"
  14. "code.gitea.io/gitea/modules/setting"
  15. "github.com/urfave/cli"
  16. )
  17. func Test_CmdKeys(t *testing.T) {
  18. onGiteaRun(t, func(*testing.T, *url.URL) {
  19. tests := []struct {
  20. name string
  21. args []string
  22. wantErr bool
  23. expectedOutput string
  24. }{
  25. {"test_empty_1", []string{"keys", "--username=git", "--type=test", "--content=test"}, true, ""},
  26. {"test_empty_2", []string{"keys", "-e", "git", "-u", "git", "-t", "test", "-k", "test"}, true, ""},
  27. {"with_key",
  28. []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="},
  29. false,
  30. "# gitea public key\ncommand=\"" + setting.AppPath + " --config=" + strconv.Quote(strconv.Quote(setting.CustomConf))[1:len(strconv.Quote(strconv.Quote(setting.CustomConf)))-1] + " 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",
  31. },
  32. {"invalid", []string{"keys", "--not-a-flag=git"}, true, "Incorrect Usage: flag provided but not defined: -not-a-flag\n\n"},
  33. }
  34. for _, tt := range tests {
  35. t.Run(tt.name, func(t *testing.T) {
  36. realStdout := os.Stdout //Backup Stdout
  37. r, w, _ := os.Pipe()
  38. os.Stdout = w
  39. set := flag.NewFlagSet("keys", 0)
  40. _ = set.Parse(tt.args)
  41. context := cli.NewContext(&cli.App{Writer: os.Stdout}, set, nil)
  42. err := cmd.CmdKeys.Run(context)
  43. if (err != nil) != tt.wantErr {
  44. t.Errorf("CmdKeys.Run() error = %v, wantErr %v", err, tt.wantErr)
  45. }
  46. w.Close()
  47. var buf bytes.Buffer
  48. io.Copy(&buf, r)
  49. commandOutput := buf.String()
  50. if tt.expectedOutput != commandOutput {
  51. t.Errorf("expectedOutput: %#v, commandOutput: %#v", tt.expectedOutput, commandOutput)
  52. }
  53. //Restore stdout
  54. os.Stdout = realStdout
  55. })
  56. }
  57. })
  58. }