forked from apache/devlake
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnection_api_test.go
More file actions
144 lines (124 loc) · 5.05 KB
/
Copy pathconnection_api_test.go
File metadata and controls
144 lines (124 loc) · 5.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package api
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/apache/incubator-devlake/core/config"
implcontext "github.com/apache/incubator-devlake/impls/context"
"github.com/apache/incubator-devlake/impls/logruslog"
"github.com/apache/incubator-devlake/plugins/jira/models"
"github.com/go-playground/validator/v10"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// initTestDeps wires up the package-level basicRes and vld.
// DefaultBasicRes satisfies context.BasicRes and only needs config + logger;
// dal can be nil because NewApiClientFromConnection never calls GetDal.
func initTestDeps(t *testing.T) {
t.Helper()
basicRes = implcontext.NewDefaultBasicRes(config.GetConfig(), logruslog.Global, nil)
vld = validator.New()
}
// serverInfoResponse is the minimal Jira serverInfo payload used in tests.
type serverInfoResponse struct {
DeploymentType string `json:"deploymentType"`
Version string `json:"version"`
VersionNumbers []int `json:"versionNumbers"`
}
// newJiraTestServer creates an httptest.Server that responds to Jira REST paths.
// It records the Authorization header sent by the client for later assertion.
func newJiraTestServer(t *testing.T, authHeaderOut *string) *httptest.Server {
t.Helper()
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if authHeaderOut != nil {
*authHeaderOut = r.Header.Get("Authorization")
}
w.Header().Set("Content-Type", "application/json")
switch {
case strings.HasSuffix(r.URL.Path, "/api/2/serverInfo"):
json.NewEncoder(w).Encode(serverInfoResponse{
DeploymentType: "Cloud",
Version: "1001.0.0",
VersionNumbers: []int{1001, 0, 0},
})
case strings.Contains(r.URL.Path, "/agile/1.0/board"):
json.NewEncoder(w).Encode(map[string]interface{}{"values": []interface{}{}})
default:
http.NotFound(w, r)
}
}))
}
// TestTestConnection_Gateway verifies that a gateway-style endpoint with
// authMethod=AccessToken sends "Authorization: Bearer <token>" and succeeds.
func TestTestConnection_Gateway(t *testing.T) {
initTestDeps(t)
var capturedAuth string
srv := newJiraTestServer(t, &capturedAuth)
defer srv.Close()
conn := models.JiraConn{}
conn.Endpoint = srv.URL + "/rest/"
conn.AuthMethod = "AccessToken"
conn.Token = "my-scoped-gateway-token"
resp, err := testConnection(context.Background(), conn)
require.Nil(t, err, "testConnection should succeed for gateway endpoint")
require.NotNil(t, resp)
assert.True(t, resp.Success)
assert.Equal(t, "Bearer my-scoped-gateway-token", capturedAuth,
"gateway mode must send Bearer token, not Basic credentials")
}
// TestTestConnection_StandardCloud verifies backward compatibility:
// a standard atlassian.net endpoint with BasicAuth still works.
func TestTestConnection_StandardCloud(t *testing.T) {
initTestDeps(t)
var capturedAuth string
srv := newJiraTestServer(t, &capturedAuth)
defer srv.Close()
conn := models.JiraConn{}
conn.Endpoint = srv.URL + "/rest/"
conn.AuthMethod = "BasicAuth"
conn.Username = "user@example.com"
conn.Password = "api-token-value"
resp, err := testConnection(context.Background(), conn)
require.Nil(t, err, "testConnection should succeed for standard cloud endpoint")
require.NotNil(t, resp)
assert.True(t, resp.Success)
assert.True(t, strings.HasPrefix(capturedAuth, "Basic "),
"standard cloud mode must send Basic auth header")
}
// TestTestConnection_NonGateway404ShowsHint verifies that when a non-gateway endpoint
// is missing /rest/ and returns 404, the helpful hint message is included in the error.
func TestTestConnection_NonGateway404ShowsHint(t *testing.T) {
initTestDeps(t)
// Server that always returns 404
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.NotFound(w, r)
}))
defer srv.Close()
// URL intentionally missing /rest/ — should trigger the "please try" hint
connMissingRest := models.JiraConn{}
connMissingRest.Endpoint = srv.URL + "/"
connMissingRest.AuthMethod = "BasicAuth"
connMissingRest.Username = "u"
connMissingRest.Password = "p"
_, errMissingRest := testConnection(context.Background(), connMissingRest)
require.NotNil(t, errMissingRest)
assert.Contains(t, errMissingRest.Error(), "please try",
"non-gateway 404 should include the /rest/ hint")
}