|
| 1 | +// Copyright 2025 The Gitea Authors. All rights reserved. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package utils |
| 5 | + |
| 6 | +import ( |
| 7 | +"net/http" |
| 8 | +"testing" |
| 9 | + |
| 10 | +"code.gitea.io/gitea/models/unittest" |
| 11 | +"code.gitea.io/gitea/modules/structs" |
| 12 | +"code.gitea.io/gitea/services/contexttest" |
| 13 | + |
| 14 | +"github.com/stretchr/testify/assert" |
| 15 | +) |
| 16 | + |
| 17 | +funcTestTestHookValidation(t*testing.T) { |
| 18 | +unittest.PrepareTestEnv(t) |
| 19 | + |
| 20 | +t.Run("Test Validation",func(t*testing.T) { |
| 21 | +ctx,_:=contexttest.MockAPIContext(t,"user2/repo1/hooks") |
| 22 | +contexttest.LoadRepo(t,ctx,1) |
| 23 | +contexttest.LoadRepoCommit(t,ctx) |
| 24 | +contexttest.LoadUser(t,ctx,2) |
| 25 | + |
| 26 | +checkCreateHookOption(ctx,&structs.CreateHookOption{ |
| 27 | +Type:"gitea", |
| 28 | +Config:map[string]string{ |
| 29 | +"content_type":"json", |
| 30 | +"url":"https://example.com/webhook", |
| 31 | +}, |
| 32 | +}) |
| 33 | +assert.Equal(t,0,ctx.Resp.WrittenStatus())// not written yet |
| 34 | +}) |
| 35 | + |
| 36 | +t.Run("Test Validation with invalid URL",func(t*testing.T) { |
| 37 | +ctx,_:=contexttest.MockAPIContext(t,"user2/repo1/hooks") |
| 38 | +contexttest.LoadRepo(t,ctx,1) |
| 39 | +contexttest.LoadRepoCommit(t,ctx) |
| 40 | +contexttest.LoadUser(t,ctx,2) |
| 41 | + |
| 42 | +checkCreateHookOption(ctx,&structs.CreateHookOption{ |
| 43 | +Type:"gitea", |
| 44 | +Config:map[string]string{ |
| 45 | +"content_type":"json", |
| 46 | +"url":"example.com/webhook", |
| 47 | +}, |
| 48 | +}) |
| 49 | +assert.Equal(t,http.StatusUnprocessableEntity,ctx.Resp.WrittenStatus()) |
| 50 | +}) |
| 51 | + |
| 52 | +t.Run("Test Validation with invalid webhook type",func(t*testing.T) { |
| 53 | +ctx,_:=contexttest.MockAPIContext(t,"user2/repo1/hooks") |
| 54 | +contexttest.LoadRepo(t,ctx,1) |
| 55 | +contexttest.LoadRepoCommit(t,ctx) |
| 56 | +contexttest.LoadUser(t,ctx,2) |
| 57 | + |
| 58 | +checkCreateHookOption(ctx,&structs.CreateHookOption{ |
| 59 | +Type:"unknown", |
| 60 | +Config:map[string]string{ |
| 61 | +"content_type":"json", |
| 62 | +"url":"example.com/webhook", |
| 63 | +}, |
| 64 | +}) |
| 65 | +assert.Equal(t,http.StatusUnprocessableEntity,ctx.Resp.WrittenStatus()) |
| 66 | +}) |
| 67 | + |
| 68 | +t.Run("Test Validation with empty content type",func(t*testing.T) { |
| 69 | +ctx,_:=contexttest.MockAPIContext(t,"user2/repo1/hooks") |
| 70 | +contexttest.LoadRepo(t,ctx,1) |
| 71 | +contexttest.LoadRepoCommit(t,ctx) |
| 72 | +contexttest.LoadUser(t,ctx,2) |
| 73 | + |
| 74 | +checkCreateHookOption(ctx,&structs.CreateHookOption{ |
| 75 | +Type:"unknown", |
| 76 | +Config:map[string]string{ |
| 77 | +"url":"https://example.com/webhook", |
| 78 | +}, |
| 79 | +}) |
| 80 | +assert.Equal(t,http.StatusUnprocessableEntity,ctx.Resp.WrittenStatus()) |
| 81 | +}) |
| 82 | +} |