|
| 1 | +/* |
| 2 | +MIT License |
| 3 | +
|
| 4 | +Copyright (c) 2023 API Testing Authors. |
| 5 | +
|
| 6 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +of this software and associated documentation files (the "Software"), to deal |
| 8 | +in the Software without restriction, including without limitation the rights |
| 9 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | +copies of the Software, and to permit persons to whom the Software is |
| 11 | +furnished to do so, subject to the following conditions: |
| 12 | +
|
| 13 | +The above copyright notice and this permission notice shall be included in all |
| 14 | +copies or substantial portions of the Software. |
| 15 | +
|
| 16 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | +SOFTWARE. |
| 23 | +*/ |
| 24 | + |
| 25 | +package runner |
| 26 | + |
| 27 | +import ( |
| 28 | + "net/http" |
| 29 | + "os" |
| 30 | + "testing" |
| 31 | + |
| 32 | + "github.com/stretchr/testify/assert" |
| 33 | + |
| 34 | + "github.com/h2non/gock" |
| 35 | +) |
| 36 | + |
| 37 | +func TestGithubPRCommentWriter(t *testing.T) { |
| 38 | + t.Run("lack of parameters", func(t *testing.T) { |
| 39 | + _, err := NewGithubPRCommentWriter(&GithubPRCommentOption{}) |
| 40 | + assert.Error(t, err) |
| 41 | + |
| 42 | + _, err = NewGithubPRCommentWriter(&GithubPRCommentOption{ |
| 43 | + Repo: "repo", |
| 44 | + }) |
| 45 | + assert.Error(t, err) |
| 46 | + |
| 47 | + _, err = NewGithubPRCommentWriter(&GithubPRCommentOption{ |
| 48 | + Identity: "id", |
| 49 | + Token: "token", |
| 50 | + }) |
| 51 | + assert.Error(t, err) |
| 52 | + |
| 53 | + _, err = NewGithubPRCommentWriter(&GithubPRCommentOption{ |
| 54 | + Repo: "repo", |
| 55 | + Identity: "id", |
| 56 | + Token: "token", |
| 57 | + }) |
| 58 | + assert.NoError(t, err) |
| 59 | + }) |
| 60 | + |
| 61 | + t.Run("pr number is invalid", func(t *testing.T) { |
| 62 | + writer, err := NewGithubPRCommentWriter(&GithubPRCommentOption{ |
| 63 | + Repo: "linuxsuren/test", |
| 64 | + Identity: "id", |
| 65 | + Token: "token", |
| 66 | + }) |
| 67 | + assert.NoError(t, err) |
| 68 | + |
| 69 | + err = writer.Output(nil) |
| 70 | + assert.NoError(t, err) |
| 71 | + assert.Nil(t, writer.WithAPIConverage(nil)) |
| 72 | + }) |
| 73 | + |
| 74 | + t.Run("error with getting comments", func(t *testing.T) { |
| 75 | + defer gock.Off() |
| 76 | + gock.New("https://api.github.com").Get("/repos/linuxsuren/test/issues/1/comments").Reply(http.StatusBadRequest) |
| 77 | + writer := createWriter(t) |
| 78 | + |
| 79 | + err := writer.Output(nil) |
| 80 | + assert.Error(t, err) |
| 81 | + }) |
| 82 | + |
| 83 | + tmpF, tErr := os.CreateTemp(os.TempDir(), "report") |
| 84 | + if !assert.NoError(t, tErr) { |
| 85 | + return |
| 86 | + } |
| 87 | + defer os.RemoveAll(tmpF.Name()) |
| 88 | + |
| 89 | + t.Run("create new comment", func(t *testing.T) { |
| 90 | + defer gock.Off() |
| 91 | + gock.New("https://api.github.com").Get("/repos/linuxsuren/test/issues/1/comments").Reply(http.StatusOK).JSON([]comment{}) |
| 92 | + gock.New("https://api.github.com").Post("/repos/linuxsuren/test/issues/1/comments").Reply(http.StatusCreated) |
| 93 | + writer := createWriterWithReport(tmpF.Name(), t) |
| 94 | + |
| 95 | + err := writer.Output([]ReportResult{{ |
| 96 | + API: "/api", |
| 97 | + }}) |
| 98 | + assert.NoError(t, err) |
| 99 | + }) |
| 100 | + |
| 101 | + t.Run("update comment", func(t *testing.T) { |
| 102 | + defer gock.Off() |
| 103 | + gock.New("https://api.github.com").Get("/repos/linuxsuren/test/issues/1/comments").Reply(http.StatusOK).JSON([]comment{{ |
| 104 | + ID: 1234, |
| 105 | + Body: "id", |
| 106 | + }}) |
| 107 | + gock.New("https://api.github.com").Patch("/repos/linuxsuren/test/issues/comments/1234").Reply(http.StatusOK) |
| 108 | + writer := createWriterWithReport(tmpF.Name(), t) |
| 109 | + |
| 110 | + err := writer.Output([]ReportResult{{ |
| 111 | + API: "/api", |
| 112 | + }}) |
| 113 | + assert.NoError(t, err) |
| 114 | + }) |
| 115 | +} |
| 116 | + |
| 117 | +func createWriter(t *testing.T) ReportResultWriter { |
| 118 | + return createWriterWithReport("", t) |
| 119 | +} |
| 120 | + |
| 121 | +func createWriterWithReport(report string, t *testing.T) ReportResultWriter { |
| 122 | + writer, err := NewGithubPRCommentWriter(&GithubPRCommentOption{ |
| 123 | + Repo: "linuxsuren/test", |
| 124 | + Identity: "id", |
| 125 | + Token: "token", |
| 126 | + PR: 1, |
| 127 | + ReportFile: report, |
| 128 | + }) |
| 129 | + assert.NoError(t, err) |
| 130 | + return writer |
| 131 | +} |
0 commit comments