11package gitlab
22
33import (
4+ "context"
5+ "fmt"
6+ "net/http"
7+ "net/http/httptest"
8+ "net/url"
49 "testing"
510
11+ "github.com/openshift-pipelines/pipelines-as-code/pkg/params/info"
12+ gl "gitlab.com/gitlab-org/api/client-go"
613 "gotest.tools/v3/assert"
714)
815
@@ -16,6 +23,7 @@ func TestExtractGitLabInfo(t *testing.T) {
1623 name : "custom host" ,
1724 url : "https://gitlab.chmouel.com/group/subgroup/repo/-/blob/main/README.md?ref_type=heads" ,
1825 expected : & gitLabInfo {
26+ Scheme : "https" ,
1927 Host : "gitlab.chmouel.com" ,
2028 GroupOrUser : "group/subgroup" ,
2129 Repository : "repo" ,
@@ -27,6 +35,7 @@ func TestExtractGitLabInfo(t *testing.T) {
2735 name : "org repo" ,
2836 url : "https://gitlab.com/org/repo/-/blob/main/README.md" ,
2937 expected : & gitLabInfo {
38+ Scheme : "https" ,
3039 Host : "gitlab.com" ,
3140 GroupOrUser : "org" ,
3241 Repository : "repo" ,
@@ -38,6 +47,7 @@ func TestExtractGitLabInfo(t *testing.T) {
3847 name : "long group and subgroups" ,
3948 url : "https://gitlab.com/gitlab-com/partners/alliance/corp/sandbox/another/foo-foo/-/raw/main/hello.txt?ref_type=heads" ,
4049 expected : & gitLabInfo {
50+ Scheme : "https" ,
4151 Host : "gitlab.com" ,
4252 GroupOrUser : "gitlab-com/partners/alliance/corp/sandbox/another" ,
4353 Repository : "foo-foo" ,
@@ -55,3 +65,59 @@ func TestExtractGitLabInfo(t *testing.T) {
5565 })
5666 }
5767}
68+
69+ func TestGetTaskURI (t * testing.T ) {
70+ ctx := context .Background ()
71+
72+ // Expected project and raw file content
73+ projectID := 12345
74+ projectSlug := "chmouel/dazgo"
75+ repoName := "dazgo"
76+ groupOrUser := "chmouel"
77+ revision := "main"
78+ filePath := "task.yaml"
79+ expectedContent := "apiVersion: tekton.dev/v1beta1\n kind: Pipeline\n metadata:\n name: test-pipeline\n spec:\n tasks:\n - name: echo-task\n taskSpec:\n steps:\n - name: echo\n image: ubuntu\n script: |\n echo \" Hello from remote pipeline!\" \n "
80+
81+ // Set up a mock HTTP server that GetTaskURI will connect to
82+ mux := http .NewServeMux ()
83+
84+ // Mock the GetProject API call
85+ mux .HandleFunc (fmt .Sprintf ("/api/v4/projects/%s" , gl .PathEscape (projectSlug )), func (w http.ResponseWriter , _ * http.Request ) {
86+ fmt .Fprintf (w , `{"id": %d, "path_with_namespace": "%s"}` , projectID , projectSlug )
87+ })
88+
89+ // Mock the GetRawFile API call
90+ mux .HandleFunc (fmt .Sprintf ("/api/v4/projects/%d/repository/files/%s/raw" , projectID , gl .PathEscape (filePath )), func (w http.ResponseWriter , r * http.Request ) {
91+ assert .Equal (t , "token" , r .Header .Get ("Private-Token" ), "Expected Private-Token header to be 'token'" )
92+ assert .Equal (t , revision , r .URL .Query ().Get ("ref" ), "Expected 'ref' query parameter to be 'main'" )
93+ fmt .Fprint (w , expectedContent )
94+ })
95+
96+ // Start the test server
97+ server := httptest .NewServer (mux )
98+ defer server .Close ()
99+
100+ // Create a GitLab provider (GetTaskURI creates its own client internally)
101+ v := & Provider {}
102+
103+ // Create an info.Event with the provider token
104+ // The URL must match the test server's host so GetTaskURI proceeds
105+ event := info .NewEvent ()
106+ event .URL = server .URL
107+ event .Provider = & info.Provider {
108+ Token : "token" ,
109+ }
110+
111+ // Parse the server URL to get the host for constructing the remote pipeline URL
112+ serverURL , err := url .Parse (server .URL )
113+ assert .NilError (t , err )
114+
115+ // The remote pipeline URL - use the test server's scheme and host
116+ remotePipelineURL := fmt .Sprintf ("%s://%s/%s/%s/-/raw/%s/%s" , serverURL .Scheme , serverURL .Host , groupOrUser , repoName , revision , filePath )
117+
118+ // Call GetTaskURI
119+ found , content , err := v .GetTaskURI (ctx , event , remotePipelineURL )
120+ assert .NilError (t , err )
121+ assert .Assert (t , found , "Expected remote pipeline to be found" )
122+ assert .Equal (t , expectedContent , content , "Returned content mismatch" )
123+ }
0 commit comments