|
| 1 | +package plugins_test |
| 2 | + |
| 3 | +import ( |
| 4 | + . "github.com/praqma/git-phlow/plugins" |
| 5 | + |
| 6 | + . "github.com/onsi/ginkgo" |
| 7 | + "net/http/httptest" |
| 8 | + "net/http" |
| 9 | + . "github.com/onsi/gomega" |
| 10 | +) |
| 11 | + |
| 12 | +var _ = Describe("Jira", func() { |
| 13 | + |
| 14 | + Describe("Testing GetJiraIssue", func() { |
| 15 | + |
| 16 | + Context("call to a valid url", func() { |
| 17 | + |
| 18 | + It("should return a jira issue object", func() { |
| 19 | + |
| 20 | + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 21 | + Ω(r.Method).Should(Equal("GET")) |
| 22 | + Ω(r.Header.Get("Content-Type")).Should(Equal("application/json")) |
| 23 | + Ω(r.URL.EscapedPath()).Should(Equal("/rest/api/latest/issue/TIS-41")) |
| 24 | + |
| 25 | + w.WriteHeader(http.StatusOK) |
| 26 | + w.Write([]byte(jiraIssueResponse)) |
| 27 | + |
| 28 | + })) |
| 29 | + defer ts.Close() |
| 30 | + |
| 31 | + issue, err := GetJiraIssue(ts.URL, "TIS-41", "user", "pass") |
| 32 | + Ω(issue.Fields.Status.Name).Should(Equal("open")) |
| 33 | + Ω(err).Should(BeNil()) |
| 34 | + }) |
| 35 | + }) |
| 36 | + |
| 37 | + Context("call to a invalid url", func() { |
| 38 | + |
| 39 | + It("send no response and should error", func() { |
| 40 | + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 41 | + Ω(r.Method).Should(Equal("GET")) |
| 42 | + Ω(r.Header.Get("Content-Type")).Should(Equal("application/json")) |
| 43 | + |
| 44 | + })) |
| 45 | + defer ts.Close() |
| 46 | + |
| 47 | + _, err := GetJiraIssue(ts.URL, "TIS-41", "user", "pass") |
| 48 | + Ω(err).ShouldNot(BeNil()) |
| 49 | + }) |
| 50 | + }) |
| 51 | + |
| 52 | + }) |
| 53 | + |
| 54 | + Describe("Testing GetTransitions", func() { |
| 55 | + |
| 56 | + Context("with a valid url", func() { |
| 57 | + |
| 58 | + It("Should return transitions object", func() { |
| 59 | + |
| 60 | + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 61 | + Ω(r.Method).Should(Equal("GET")) |
| 62 | + Ω(r.Header.Get("Content-Type")).Should(Equal("application/json")) |
| 63 | + Ω(r.URL.EscapedPath()).Should(Equal("/rest/api/latest/issue/TIS-41/transitions")) |
| 64 | + |
| 65 | + w.WriteHeader(http.StatusOK) |
| 66 | + w.Write([]byte(jiraTransitionResponse)) |
| 67 | + |
| 68 | + })) |
| 69 | + defer ts.Close() |
| 70 | + |
| 71 | + transitions, err := GetTransitions(ts.URL, "TIS-41", "user", "pass") |
| 72 | + Ω(transitions.Transitions[0].To.Name).Should(Equal("In Progress")) |
| 73 | + Ω(err).Should(BeNil()) |
| 74 | + }) |
| 75 | + |
| 76 | + }) |
| 77 | + |
| 78 | + }) |
| 79 | + |
| 80 | + Describe("Testing DoTransition", func() { |
| 81 | + |
| 82 | + Context("Transition with a valid transition ID", func() { |
| 83 | + |
| 84 | + //Consult API documentation for status codes - https://docs.atlassian.com/jira/REST/cloud/#api/2/issue-doTransition |
| 85 | + It("Should return status code 204 ", func() { |
| 86 | + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 87 | + Ω(r.Method).Should(Equal("POST")) |
| 88 | + Ω(r.Header.Get("Content-Type")).Should(Equal("application/json")) |
| 89 | + Ω(r.URL.EscapedPath()).Should(Equal("/rest/api/latest/issue/TIS-41/transitions")) |
| 90 | + |
| 91 | + w.WriteHeader(204) |
| 92 | + w.Write([]byte(jiraTransitionResponse)) |
| 93 | + |
| 94 | + })) |
| 95 | + defer ts.Close() |
| 96 | + |
| 97 | + err := DoTransition(ts.URL, "TIS-41", "user", "pass", "11") |
| 98 | + Ω(err).Should(BeNil()) |
| 99 | + }) |
| 100 | + |
| 101 | + }) |
| 102 | + |
| 103 | + Context("Transition with an invalid transition ID", func() { |
| 104 | + |
| 105 | + //Consult API documentation for status codes - https://docs.atlassian.com/jira/REST/cloud/#api/2/issue-doTransition |
| 106 | + It("Should return status code 400 ", func() { |
| 107 | + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 108 | + Ω(r.Method).Should(Equal("POST")) |
| 109 | + Ω(r.Header.Get("Content-Type")).Should(Equal("application/json")) |
| 110 | + Ω(r.URL.EscapedPath()).Should(Equal("/rest/api/latest/issue/TIS-41/transitions")) |
| 111 | + |
| 112 | + w.WriteHeader(400) |
| 113 | + w.Write([]byte("error")) |
| 114 | + |
| 115 | + })) |
| 116 | + defer ts.Close() |
| 117 | + |
| 118 | + err := DoTransition(ts.URL, "TIS-41", "user", "pass", "111") |
| 119 | + Ω(err).ShouldNot(BeNil()) |
| 120 | + }) |
| 121 | + |
| 122 | + }) |
| 123 | + |
| 124 | + }) |
| 125 | + |
| 126 | + Describe("Testing assign user", func() { |
| 127 | + |
| 128 | + Context("Assign with a valid user", func() { |
| 129 | + It("Should return status code 204 ", func() { |
| 130 | + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 131 | + Ω(r.Method).Should(Equal("PUT")) |
| 132 | + Ω(r.Header.Get("Content-Type")).Should(Equal("application/json")) |
| 133 | + Ω(r.URL.EscapedPath()).Should(Equal("/rest/api/latest/issue/TIS-41/assignee")) |
| 134 | + |
| 135 | + w.WriteHeader(204) |
| 136 | + w.Write([]byte(jiraTransitionResponse)) |
| 137 | + |
| 138 | + })) |
| 139 | + defer ts.Close() |
| 140 | + |
| 141 | + err := AssignUser(ts.URL, "TIS-41", "admin", "pass") |
| 142 | + Ω(err).Should(BeNil()) |
| 143 | + }) |
| 144 | + }) |
| 145 | + }) |
| 146 | +}) |
0 commit comments