Skip to content

Commit

Permalink
bug(repo sync): add tests for isWebhookDel bool
Browse files Browse the repository at this point in the history
  • Loading branch information
KellyMerrick committed Sep 6, 2023
1 parent 115d436 commit f89e929
Showing 1 changed file with 39 additions and 16 deletions.
55 changes: 39 additions & 16 deletions scm/github/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,7 @@ func TestGithub_Update(t *testing.T) {
}
}

func TestGithub_Update_NotFoundHook(t *testing.T) {
func TestGithub_Update_isWebhookDel_False(t *testing.T) {
// setup context
gin.SetMode(gin.TestMode)

Expand All @@ -694,8 +694,7 @@ func TestGithub_Update_NotFoundHook(t *testing.T) {
// setup mock server
engine.PATCH("/api/v3/repos/:org/:repo/hooks/:hook_id", func(c *gin.Context) {
c.Header("Content-Type", "application/json")
c.Status(http.StatusNotFound)
// c.File("testdata/hook.json")
c.Status(http.StatusOK)
})

s := httptest.NewServer(engine)
Expand All @@ -707,29 +706,53 @@ func TestGithub_Update_NotFoundHook(t *testing.T) {
u.SetToken("bar")

r := new(library.Repo)
r.SetID(1)
r.SetName("bar")
r.SetOrg("foo")
r.SetHash("secret")
r.SetAllowPush(true)
r.SetAllowPull(true)
r.SetAllowDeploy(true)

hookID := int64(0)
client, _ := NewTest(s.URL)

// run test
isWebhookDel, err := client.Update(u, r, 0)

if isWebhookDel {
t.Errorf("Update returned %v, want %v", isWebhookDel, false)
}

if err != nil {
t.Errorf("Update returned err: %v", err)
}
}

func TestGithub_Update_isWebhookDel_True(t *testing.T) {
// setup context
gin.SetMode(gin.TestMode)

resp := httptest.NewRecorder()
_, engine := gin.CreateTestContext(resp)

// setup mock server
engine.PATCH("/api/v3/repos/:org/:repo/hooks/:hook_id", func(c *gin.Context) {
c.Header("Content-Type", "application/json")
c.Status(http.StatusNotFound)
})

s := httptest.NewServer(engine)
defer s.Close()

// setup types
u := new(library.User)
u.SetName("foo")
u.SetToken("bar")

r := new(library.Repo)

client, _ := NewTest(s.URL)

// run test
isWebhookDel, err := client.Update(u, r, hookID)
isWebhookDel, err := client.Update(u, r, 0)

if !isWebhookDel {
t.Errorf("Update returned %v, want %v", isWebhookDel, true)
}

if resp.Code != http.StatusNotFound {
t.Errorf("Update returned %v, want %v", resp.Code, http.StatusNotFound)
}

if err == nil {
t.Error("Update should return error")
}
Expand Down

0 comments on commit f89e929

Please sign in to comment.