user_delete_test.go 2.1 KB
Newer Older
1 2 3 4 5
package cmd

import (
	"bytes"
	"github.com/golang/mock/gomock"
6
	"github.com/jenkins-zh/jenkins-cli/client"
7
	"github.com/jenkins-zh/jenkins-cli/mock/mhttp"
8 9
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
10 11
	"io/ioutil"
	"os"
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
)

var _ = Describe("user delete command", func() {
	var (
		ctrl         *gomock.Controller
		roundTripper *mhttp.MockRoundTripper
	)

	BeforeEach(func() {
		ctrl = gomock.NewController(GinkgoT())
		rootCmd.SetArgs([]string{})
		rootOptions.Jenkins = ""
		rootOptions.ConfigFile = "test.yaml"
	})

	AfterEach(func() {
		rootCmd.SetArgs([]string{})
		os.Remove(rootOptions.ConfigFile)
		rootOptions.ConfigFile = ""
		ctrl.Finish()
	})

	Context("with http requests", func() {
		BeforeEach(func() {
			roundTripper = mhttp.NewMockRoundTripper(ctrl)
			userDeleteOption.RoundTripper = roundTripper
		})

		It("should success", func() {
LinuxSuRen's avatar
LinuxSuRen 已提交
41
			data, err := GenerateSampleConfig()
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
			Expect(err).To(BeNil())
			err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664)
			Expect(err).To(BeNil())

			targetUserName := "fakename"
			client.PrepareForDeleteUser(roundTripper, "http://localhost:8080/jenkins", targetUserName, "admin", "111e3a2f0231198855dceaff96f20540a9")

			rootCmd.SetArgs([]string{"user", "delete", targetUserName, "-b", "true"})

			buf := new(bytes.Buffer)
			rootCmd.SetOutput(buf)
			_, err = rootCmd.ExecuteC()
			Expect(err).To(BeNil())

			Expect(buf.String()).To(Equal(""))
		})

		It("with status code 500", func() {
LinuxSuRen's avatar
LinuxSuRen 已提交
60
			data, err := GenerateSampleConfig()
61 62 63 64 65 66 67 68 69 70 71 72 73
			Expect(err).To(BeNil())
			err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664)
			Expect(err).To(BeNil())

			targetUserName := "fakename"
			response := client.PrepareForDeleteUser(roundTripper, "http://localhost:8080/jenkins", targetUserName, "admin", "111e3a2f0231198855dceaff96f20540a9")
			response.StatusCode = 500

			rootCmd.SetArgs([]string{"user", "delete", targetUserName, "-b", "true"})

			buf := new(bytes.Buffer)
			rootCmd.SetOutput(buf)
			_, err = rootCmd.ExecuteC()
74 75
			Expect(err).To(HaveOccurred())
			Expect(err.Error()).To(ContainSubstring("unexpected status code: 500"))
76 77 78
		})
	})
})