From 8bea92c3dc162e24f6dcc2902dfed5ab94576826 Mon Sep 17 00:00:00 2001 From: Antoine GIRARD Date: Sun, 15 Dec 2019 09:11:31 +0100 Subject: [PATCH] test: command keys (#9357) --- integrations/cmd_keys_test.go | 62 ++++++++++++++++++++++++++++++++++ models/fixtures/public_key.yml | 12 ++++++- 2 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 integrations/cmd_keys_test.go diff --git a/integrations/cmd_keys_test.go b/integrations/cmd_keys_test.go new file mode 100644 index 00000000000..4294c319901 --- /dev/null +++ b/integrations/cmd_keys_test.go @@ -0,0 +1,62 @@ +// Copyright 2019 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package integrations + +import ( + "bytes" + "flag" + "io" + "os" + "testing" + + "code.gitea.io/gitea/cmd" + "code.gitea.io/gitea/modules/setting" + + "github.com/urfave/cli" +) + +func Test_CmdKeys(t *testing.T) { + defer prepareTestEnv(t)() + + tests := []struct { + name string + args []string + wantErr bool + expectedOutput string + }{ + {"test_empty_1", []string{"keys", "--username=git", "--type=test", "--content=test"}, true, ""}, + {"test_empty_2", []string{"keys", "-e", "git", "-u", "git", "-t", "test", "-k", "test"}, true, ""}, + {"with_key", + []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="}, + false, + "# 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", + }, + {"invalid", []string{"keys", "--not-a-flag=git"}, true, "Incorrect Usage: flag provided but not defined: -not-a-flag\n\n"}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + realStdout := os.Stdout //Backup Stdout + r, w, _ := os.Pipe() + os.Stdout = w + + set := flag.NewFlagSet("keys", 0) + _ = set.Parse(tt.args) + context := cli.NewContext(&cli.App{Writer: os.Stdout}, set, nil) + err := cmd.CmdKeys.Run(context) + if (err != nil) != tt.wantErr { + t.Errorf("CmdKeys.Run() error = %v, wantErr %v", err, tt.wantErr) + } + w.Close() + var buf bytes.Buffer + io.Copy(&buf, r) + commandOutput := buf.String() + if tt.expectedOutput != commandOutput { + t.Errorf("expectedOutput: %#v, commandOutput: %#v", tt.expectedOutput, commandOutput) + } + //Restore stdout + os.Stdout = realStdout + }) + } +} diff --git a/models/fixtures/public_key.yml b/models/fixtures/public_key.yml index ca780a73aa0..08f5c349a7b 100644 --- a/models/fixtures/public_key.yml +++ b/models/fixtures/public_key.yml @@ -1 +1,11 @@ -[] # empty +- + id: 1 + owner_id: 2 + name: user2@localhost + fingerprint: "SHA256:M3iiFbqQKgLxi+WAoRa38ZVQ9ktdfau2sOu9xuPb9ew" + content: "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDWVj0fQ5N8wNc0LVNA41wDLYJ89ZIbejrPfg/avyj3u/ZohAKsQclxG4Ju0VirduBFF9EOiuxoiFBRr3xRpqzpsZtnMPkWVWb+akZwBFAx8p+jKdy4QXR/SZqbVobrGwip2UjSrri1CtBxpJikojRIZfCnDaMOyd9Jp6KkujvniFzUWdLmCPxUE9zhTaPu0JsEP7MW0m6yx7ZUhHyfss+NtqmFTaDO+QlMR7L2QkDliN2Jl3Xa3PhuWnKJfWhdAq1Cw4oraKUOmIgXLkuiuxVQ6mD3AiFupkmfqdHq6h+uHHmyQqv3gU+/sD8GbGAhf6ftqhTsXjnv1Aj4R8NoDf9BS6KRkzkeun5UisSzgtfQzjOMEiJtmrep2ZQrMGahrXa+q4VKr0aKJfm+KlLfwm/JztfsBcqQWNcTURiCFqz+fgZw0Ey/de0eyMzldYTdXXNRYCKjs9bvBK+6SSXRM7AhftfQ0ZuoW5+gtinPrnmoOaSCEJbAiEiTO/BzOHgowiM= user2@localhost" + mode: 2 + type: 1 + created_unix: 1559593109 + updated_unix: 1565224552 + login_source_id: 0 \ No newline at end of file