@@ -2,12 +2,17 @@ package atlassian
22
33import (
44 "context"
5+ "fmt"
6+ "net/http"
57 "testing"
68
79 "github.com/google/go-cmp/cmp"
10+ "github.com/stretchr/testify/assert"
811 "github.com/stretchr/testify/require"
12+ "github.com/trufflesecurity/trufflehog/v3/pkg/common"
913 "github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
1014 "github.com/trufflesecurity/trufflehog/v3/pkg/engine/ahocorasick"
15+ "gopkg.in/h2non/gock.v1"
1116)
1217
1318func TestAtlassian_Pattern (t * testing.T ) {
@@ -79,3 +84,80 @@ func TestAtlassian_Pattern(t *testing.T) {
7984 })
8085 }
8186}
87+
88+ // TestAtlassian_AnalysisInfo_KeyAndOrgId tests if both the key and organization id are populated into AnalysisInfo
89+ // given that they are present in the input data chunk
90+ func TestAtlassian_AnalysisInfo_KeyAndOrgId (t * testing.T ) {
91+ client := common .SaneHttpClient ()
92+ d := Scanner {client : client }
93+
94+ key := "ATCTT3xFfGN0GsZNgOGrQSHSnxiJVi00oHlRicyM0yMNuKCBfw6qOHVcCy4Hm89GnclGb_W-1qAkxqCn5XbuyoX54bNhpK5yFKGFR7ocV6FByvL_P9Sb3tFnbUg3T3I3S_RGCBLMSN7Nsa4GJv8JEJ6bzvDmX-oJ8AnrazMU-zZ5hb-u3t2ERew=366BFE3A"
95+ orgId := "123j4567-e89b-12d3-a456-426614174000"
96+
97+ defer gock .Off ()
98+ defer gock .RestoreClient (client )
99+ gock .InterceptClient (client )
100+ gock .New ("https://api.atlassian.com" ).
101+ Get ("/admin/v1/orgs" ).
102+ MatchHeader ("Accept" , "application/json" ).
103+ MatchHeader ("Authorization" , fmt .Sprintf ("Bearer %s" , key )).
104+ Reply (http .StatusOK ).
105+ JSON (map [string ]any {
106+ "Data" : []map [string ]any {},
107+ })
108+
109+ t .Run ("key and organization id both present" , func (t * testing.T ) {
110+ input := fmt .Sprintf (`
111+ [INFO] Sending request to the atlassian API
112+ [DEBUG] Using Key=%s
113+ [DEBUG] Using Organization ID=%s
114+ [INFO] Response received: 200 OK
115+ ` , key , orgId )
116+
117+ results , err := d .FromData (context .Background (), true , []byte (input ))
118+ require .NoError (t , err )
119+ require .Len (t , results , 1 , "mismatch in result count: expected %d, got %d" , 1 , len (results ))
120+ result := results [0 ]
121+ require .NotNil (t , result .AnalysisInfo , "AnalysisInfo is nil" )
122+
123+ assert .Equal (t , key , result .AnalysisInfo ["key" ], "mismatch in key" )
124+ assert .Equal (t , orgId , result .AnalysisInfo ["organization_id" ], "mismatch in organization_id" )
125+ })
126+ }
127+
128+ // TestAtlassian_AnalysisInfo_KeyOnly tests if only key is populated into AnalysisInfo
129+ // given that only the key and no organization_id is present in the input data chunk
130+ func TestAtlassian_AnalysisInfo_KeyOnly (t * testing.T ) {
131+ client := common .SaneHttpClient ()
132+ d := Scanner {client : client }
133+
134+ key := "ATCTT3xFfGN0GsZNgOGrQSHSnxiJVi00oHlRicyM0yMNuKCBfw6qOHVcCy4Hm89GnclGb_W-1qAkxqCn5XbuyoX54bNhpK5yFKGFR7ocV6FByvL_P9Sb3tFnbUg3T3I3S_RGCBLMSN7Nsa4GJv8JEJ6bzvDmX-oJ8AnrazMU-zZ5hb-u3t2ERew=366BFE3A"
135+
136+ defer gock .Off ()
137+ defer gock .RestoreClient (client )
138+ gock .InterceptClient (client )
139+ gock .New ("https://api.atlassian.com" ).
140+ Get ("/admin/v1/orgs" ).
141+ MatchHeader ("Accept" , "application/json" ).
142+ MatchHeader ("Authorization" , fmt .Sprintf ("Bearer %s" , key )).
143+ Reply (http .StatusOK ).
144+ JSON (map [string ]any {
145+ "Data" : []map [string ]any {},
146+ })
147+ t .Run ("only key present" , func (t * testing.T ) {
148+
149+ input := fmt .Sprintf (`
150+ [INFO] Sending request to the atlassian API
151+ [DEBUG] Using Key=%s
152+ [INFO] Response received: 200 OK
153+ ` , key )
154+
155+ results , err := d .FromData (context .Background (), true , []byte (input ))
156+ require .NoError (t , err )
157+ require .Len (t , results , 1 , "mismatch in result count: expected %d, got %d" , 1 , len (results ))
158+ result := results [0 ]
159+ require .NotNil (t , result .AnalysisInfo , "AnalysisInfo is nil" )
160+
161+ assert .Equal (t , key , result .AnalysisInfo ["key" ], "mismatch in key" )
162+ })
163+ }
0 commit comments