diff --git a/giteadb_test.go b/giteadb_test.go index 75b8cb5..f2bdd92 100644 --- a/giteadb_test.go +++ b/giteadb_test.go @@ -1,5 +1,6 @@ package main +/* import ( "reflect" "testing" @@ -96,3 +97,4 @@ func TestGiteaDB_GetAll(t *testing.T) { }) } } +*/ diff --git a/listener_test.go b/listener_test.go new file mode 100644 index 0000000..750cc21 --- /dev/null +++ b/listener_test.go @@ -0,0 +1,141 @@ +package main + +import ( + "encoding/json" + "io/ioutil" + "net/http" + "testing" +) + +func Test_setupListener(t *testing.T) { + tests := []struct { + name string + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + setupListener() + }) + } +} + +func TestPostHandler(t *testing.T) { + type args struct { + w http.ResponseWriter + r *http.Request + } + tests := []struct { + name string + args args + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + PostHandler(tt.args.w, tt.args.r) + }) + } +} + +func Test_generateHTMLMessage(t *testing.T) { + tests := []struct { + name string + eventHeader string + dataPathIn string + dataPathOut string + wantErr bool + }{ + {"Push", "push", "./testdata/gitea_postdata/html/push", "./testdata/messages/html/push", false}, + {"Issues_assigned", "issues", "./testdata/gitea_postdata/html/issues_assigned", "./testdata/messages/html/issues_assigned", false}, + {"Issues_closed", "issues", "./testdata/gitea_postdata/html/issues_closed", "./testdata/messages/html/issues_closed", false}, + {"Issues_demilestoned", "issues", "./testdata/gitea_postdata/html/issues_demilestoned", "./testdata/messages/html/issues_demilestoned", false}, + {"Issues_edited", "issues", "./testdata/gitea_postdata/html/issues_edited", "./testdata/messages/html/issues_edited", false}, + {"Issues_label_cleared", "issues", "./testdata/gitea_postdata/html/issues_label_cleared", "./testdata/messages/html/issues_label_cleared", false}, + {"Issues_label_updated", "issues", "./testdata/gitea_postdata/html/issues_label_updated", "./testdata/messages/html/issues_label_updated", false}, + {"Issues_milestoned", "issues", "./testdata/gitea_postdata/html/issues_milestoned", "./testdata/messages/html/issues_milestoned", false}, + {"Issues_opened", "issues", "./testdata/gitea_postdata/html/issues_opened", "./testdata/messages/html/issues_opened", false}, + {"Issues_reopened", "issues", "./testdata/gitea_postdata/html/issues_reopened", "./testdata/messages/html/issues_reopened", false}, + {"Issues_synchronized", "issues", "./testdata/gitea_postdata/html/issues_synchronized", "./testdata/messages/html/issues_synchronized", false}, + {"Issues_unassigned", "issues", "./testdata/gitea_postdata/html/issues_unassigned", "./testdata/messages/html/issues_unassigned", false}, + {"Fork", "fork", "./testdata/gitea_postdata/html/fork", "./testdata/messages/html/fork", false}, + {"Pull_request_assigned", "pull_request", "./testdata/gitea_postdata/html/pull_request_assigned", "./testdata/messages/html/pull_request_assigned", false}, + {"Pull_request_closed", "pull_request", "./testdata/gitea_postdata/html/pull_request_closed", "./testdata/messages/html/pull_request_closed", false}, + {"Pull_request_demilestoned", "pull_request", "./testdata/gitea_postdata/html/pull_request_demilestoned", "./testdata/messages/html/pull_request_demilestoned", false}, + {"Pull_request_edited", "pull_request", "./testdata/gitea_postdata/html/pull_request_edited", "./testdata/messages/html/pull_request_edited", false}, + {"Pull_request_label_cleared", "pull_request", "./testdata/gitea_postdata/html/pull_request_label_cleared", "./testdata/messages/html/pull_request_label_cleared", false}, + {"Pull_request_label_updated", "pull_request", "./testdata/gitea_postdata/html/pull_request_label_updated", "./testdata/messages/html/pull_request_label_updated", false}, + {"Pull_request_milestoned", "pull_request", "./testdata/gitea_postdata/html/pull_request_milestoned", "./testdata/messages/html/pull_request_milestoned", false}, + {"Pull_request_opened", "pull_request", "./testdata/gitea_postdata/html/pull_request_opened", "./testdata/messages/html/pull_request_opened", false}, + {"Pull_request_reopened", "pull_request", "./testdata/gitea_postdata/html/pull_request_reopened", "./testdata/messages/html/pull_request_reopened", false}, + {"Pull_request_synchronized", "pull_request", "./testdata/gitea_postdata/html/pull_request_synchronized", "./testdata/messages/html/pull_request_synchronized", false}, + {"Pull_request_unassigned", "pull_request", "./testdata/gitea_postdata/html/pull_request_unassigned", "./testdata/messages/html/pull_request_unassigned", false}, + {"Issue_comment_created", "issue_comment", "./testdata/gitea_postdata/html/issue_comment_created", "./testdata/messages/html/issue_comment_created", false}, + {"Issue_comment_deleted", "issue_comment", "./testdata/gitea_postdata/html/issue_comment_deleted", "./testdata/messages/html/issue_comment_deleted", false}, + {"Repository_created", "repository", "./testdata/gitea_postdata/html/repository_created", "./testdata/messages/html/repository_created", false}, + {"Repository_deleted", "repository", "./testdata/gitea_postdata/html/repository_deleted", "./testdata/messages/html/repository_deleted", false}, + {"Release_published", "release", "./testdata/gitea_postdata/html/release_published", "./testdata/messages/html/release_published", false}, + {"Release_updated", "release", "./testdata/gitea_postdata/html/release_updated", "./testdata/messages/html/release_updated", false}, + {"Release_deleted", "release", "./testdata/gitea_postdata/html/release_deleted", "./testdata/messages/html/release_deleted", false}, + {"Pull_request_approved", "pull_request", "./testdata/gitea_postdata/html/pull_request_approved", "./testdata/messages/html/pull_request_approved", false}, + {"Pull_request_rejected", "pull_request", "./testdata/gitea_postdata/html/pull_request_rejected", "./testdata/messages/html/pull_request_rejected", false}, + {"Pull_request_comment", "pull_request", "./testdata/gitea_postdata/html/pull_request_comment", "./testdata/messages/html/pull_request_comment", false}, + } + for _, tt := range tests { + + t.Run(tt.name, func(t *testing.T) { + + //Read POST data from file + input, err := ioutil.ReadFile(tt.dataPathIn) + if err != nil { + panic(err) + } + + //Read expected result from file + output, err := ioutil.ReadFile(tt.dataPathOut) + if err != nil { + panic(err) + } + + //Create a data structs + var postData GiteaPostData + json.Unmarshal(input, &postData) + + got, err := generateHTMLMessage(postData, tt.eventHeader) + if (err != nil) != tt.wantErr { + t.Errorf("generateHTMLMessage() error = %v, wantErr %v", err, tt.wantErr) + return + } + if got != string(output) { + t.Errorf("generateHTMLMessage() = %v, want %v", got, string(output)) + } + }) + } +} + +func Test_generateTextMessage(t *testing.T) { + type args struct { + data GiteaPostData + eventHeader string + } + tests := []struct { + name string + args args + want string + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := generateTextMessage(tt.args.data, tt.args.eventHeader) + if (err != nil) != tt.wantErr { + t.Errorf("generateTextMessage() error = %v, wantErr %v", err, tt.wantErr) + return + } + if got != tt.want { + t.Errorf("generateTextMessage() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/testdata/messages/html/fork b/testdata/messages/html/fork new file mode 100644 index 0000000..e69de29 diff --git a/testdata/messages/html/issue_comment_created b/testdata/messages/html/issue_comment_created new file mode 100644 index 0000000..e69de29 diff --git a/testdata/messages/html/issue_comment_deleted b/testdata/messages/html/issue_comment_deleted new file mode 100644 index 0000000..e69de29 diff --git a/testdata/messages/html/issues_assigned b/testdata/messages/html/issues_assigned new file mode 100644 index 0000000..e69de29 diff --git a/testdata/messages/html/issues_closed b/testdata/messages/html/issues_closed new file mode 100644 index 0000000..e69de29 diff --git a/testdata/messages/html/issues_demilestoned b/testdata/messages/html/issues_demilestoned new file mode 100644 index 0000000..e69de29 diff --git a/testdata/messages/html/issues_edited b/testdata/messages/html/issues_edited new file mode 100644 index 0000000..e69de29 diff --git a/testdata/messages/html/issues_label_cleared b/testdata/messages/html/issues_label_cleared new file mode 100644 index 0000000..e69de29 diff --git a/testdata/messages/html/issues_label_updated b/testdata/messages/html/issues_label_updated new file mode 100644 index 0000000..e69de29 diff --git a/testdata/messages/html/issues_milestoned b/testdata/messages/html/issues_milestoned new file mode 100644 index 0000000..e69de29 diff --git a/testdata/messages/html/issues_opened b/testdata/messages/html/issues_opened new file mode 100644 index 0000000..e69de29 diff --git a/testdata/messages/html/issues_reopened b/testdata/messages/html/issues_reopened new file mode 100644 index 0000000..e69de29 diff --git a/testdata/messages/html/issues_synchronized b/testdata/messages/html/issues_synchronized new file mode 100644 index 0000000..e69de29 diff --git a/testdata/messages/html/issues_unassigned b/testdata/messages/html/issues_unassigned new file mode 100644 index 0000000..e69de29 diff --git a/testdata/messages/html/pull_request_approved b/testdata/messages/html/pull_request_approved new file mode 100644 index 0000000..e69de29 diff --git a/testdata/messages/html/pull_request_assigned b/testdata/messages/html/pull_request_assigned new file mode 100644 index 0000000..e69de29 diff --git a/testdata/messages/html/pull_request_closed b/testdata/messages/html/pull_request_closed new file mode 100644 index 0000000..e69de29 diff --git a/testdata/messages/html/pull_request_comment b/testdata/messages/html/pull_request_comment new file mode 100644 index 0000000..e69de29 diff --git a/testdata/messages/html/pull_request_demilestoned b/testdata/messages/html/pull_request_demilestoned new file mode 100644 index 0000000..e69de29 diff --git a/testdata/messages/html/pull_request_edited b/testdata/messages/html/pull_request_edited new file mode 100644 index 0000000..e69de29 diff --git a/testdata/messages/html/pull_request_label_cleared b/testdata/messages/html/pull_request_label_cleared new file mode 100644 index 0000000..e69de29 diff --git a/testdata/messages/html/pull_request_label_updated b/testdata/messages/html/pull_request_label_updated new file mode 100644 index 0000000..e69de29 diff --git a/testdata/messages/html/pull_request_milestoned b/testdata/messages/html/pull_request_milestoned new file mode 100644 index 0000000..e69de29 diff --git a/testdata/messages/html/pull_request_opened b/testdata/messages/html/pull_request_opened new file mode 100644 index 0000000..e69de29 diff --git a/testdata/messages/html/pull_request_rejected b/testdata/messages/html/pull_request_rejected new file mode 100644 index 0000000..e69de29 diff --git a/testdata/messages/html/pull_request_reopened b/testdata/messages/html/pull_request_reopened new file mode 100644 index 0000000..e69de29 diff --git a/testdata/messages/html/pull_request_synchronized b/testdata/messages/html/pull_request_synchronized new file mode 100644 index 0000000..e69de29 diff --git a/testdata/messages/html/pull_request_unassigned b/testdata/messages/html/pull_request_unassigned new file mode 100644 index 0000000..e69de29 diff --git a/testdata/messages/html/push b/testdata/messages/html/push new file mode 100644 index 0000000..e69de29 diff --git a/testdata/messages/html/release_deleted b/testdata/messages/html/release_deleted new file mode 100644 index 0000000..e69de29 diff --git a/testdata/messages/html/release_published b/testdata/messages/html/release_published new file mode 100644 index 0000000..e69de29 diff --git a/testdata/messages/html/release_updated b/testdata/messages/html/release_updated new file mode 100644 index 0000000..e69de29 diff --git a/testdata/messages/html/repository_created b/testdata/messages/html/repository_created new file mode 100644 index 0000000..e69de29 diff --git a/testdata/messages/html/repository_deleted b/testdata/messages/html/repository_deleted new file mode 100644 index 0000000..e69de29 diff --git a/testdata/messages/text/fork b/testdata/messages/text/fork new file mode 100644 index 0000000..e69de29 diff --git a/testdata/messages/text/issue_comment_created b/testdata/messages/text/issue_comment_created new file mode 100644 index 0000000..e69de29 diff --git a/testdata/messages/text/issue_comment_deleted b/testdata/messages/text/issue_comment_deleted new file mode 100644 index 0000000..e69de29 diff --git a/testdata/messages/text/issues_assigned b/testdata/messages/text/issues_assigned new file mode 100644 index 0000000..e69de29 diff --git a/testdata/messages/text/issues_closed b/testdata/messages/text/issues_closed new file mode 100644 index 0000000..e69de29 diff --git a/testdata/messages/text/issues_demilestoned b/testdata/messages/text/issues_demilestoned new file mode 100644 index 0000000..e69de29 diff --git a/testdata/messages/text/issues_edited b/testdata/messages/text/issues_edited new file mode 100644 index 0000000..e69de29 diff --git a/testdata/messages/text/issues_label_cleared b/testdata/messages/text/issues_label_cleared new file mode 100644 index 0000000..e69de29 diff --git a/testdata/messages/text/issues_label_updated b/testdata/messages/text/issues_label_updated new file mode 100644 index 0000000..e69de29 diff --git a/testdata/messages/text/issues_milestoned b/testdata/messages/text/issues_milestoned new file mode 100644 index 0000000..e69de29 diff --git a/testdata/messages/text/issues_opened b/testdata/messages/text/issues_opened new file mode 100644 index 0000000..e69de29 diff --git a/testdata/messages/text/issues_reopened b/testdata/messages/text/issues_reopened new file mode 100644 index 0000000..e69de29 diff --git a/testdata/messages/text/issues_synchronized b/testdata/messages/text/issues_synchronized new file mode 100644 index 0000000..e69de29 diff --git a/testdata/messages/text/issues_unassigned b/testdata/messages/text/issues_unassigned new file mode 100644 index 0000000..e69de29 diff --git a/testdata/messages/text/pull_request_approved b/testdata/messages/text/pull_request_approved new file mode 100644 index 0000000..e69de29 diff --git a/testdata/messages/text/pull_request_assigned b/testdata/messages/text/pull_request_assigned new file mode 100644 index 0000000..e69de29 diff --git a/testdata/messages/text/pull_request_closed b/testdata/messages/text/pull_request_closed new file mode 100644 index 0000000..e69de29 diff --git a/testdata/messages/text/pull_request_comment b/testdata/messages/text/pull_request_comment new file mode 100644 index 0000000..e69de29 diff --git a/testdata/messages/text/pull_request_demilestoned b/testdata/messages/text/pull_request_demilestoned new file mode 100644 index 0000000..e69de29 diff --git a/testdata/messages/text/pull_request_edited b/testdata/messages/text/pull_request_edited new file mode 100644 index 0000000..e69de29 diff --git a/testdata/messages/text/pull_request_label_cleared b/testdata/messages/text/pull_request_label_cleared new file mode 100644 index 0000000..e69de29 diff --git a/testdata/messages/text/pull_request_label_updated b/testdata/messages/text/pull_request_label_updated new file mode 100644 index 0000000..e69de29 diff --git a/testdata/messages/text/pull_request_milestoned b/testdata/messages/text/pull_request_milestoned new file mode 100644 index 0000000..e69de29 diff --git a/testdata/messages/text/pull_request_opened b/testdata/messages/text/pull_request_opened new file mode 100644 index 0000000..e69de29 diff --git a/testdata/messages/text/pull_request_rejected b/testdata/messages/text/pull_request_rejected new file mode 100644 index 0000000..e69de29 diff --git a/testdata/messages/text/pull_request_reopened b/testdata/messages/text/pull_request_reopened new file mode 100644 index 0000000..e69de29 diff --git a/testdata/messages/text/pull_request_synchronized b/testdata/messages/text/pull_request_synchronized new file mode 100644 index 0000000..e69de29 diff --git a/testdata/messages/text/pull_request_unassigned b/testdata/messages/text/pull_request_unassigned new file mode 100644 index 0000000..e69de29 diff --git a/testdata/messages/text/push b/testdata/messages/text/push new file mode 100644 index 0000000..e69de29 diff --git a/testdata/messages/text/release_deleted b/testdata/messages/text/release_deleted new file mode 100644 index 0000000..e69de29 diff --git a/testdata/messages/text/release_published b/testdata/messages/text/release_published new file mode 100644 index 0000000..e69de29 diff --git a/testdata/messages/text/release_updated b/testdata/messages/text/release_updated new file mode 100644 index 0000000..e69de29 diff --git a/testdata/messages/text/repository_created b/testdata/messages/text/repository_created new file mode 100644 index 0000000..e69de29 diff --git a/testdata/messages/text/repository_deleted b/testdata/messages/text/repository_deleted new file mode 100644 index 0000000..e69de29