-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
tests(pypi): add tests for fetching package from pypi #39
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,11 +5,13 @@ package worker | |
import ( | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"reflect" | ||
"strings" | ||
|
||
"github.com/opensbom-generator/parsers/internal/helper" | ||
"github.com/opensbom-generator/parsers/meta" | ||
) | ||
|
||
|
@@ -82,32 +84,36 @@ var HashAlgoPickOrder []meta.HashAlgorithm = []meta.HashAlgorithm{ | |
meta.HashAlgoMD2, | ||
} | ||
|
||
func makeGetRequest(packageJSONURL string) (*http.Response, error) { | ||
url := "https://" + packageJSONURL | ||
|
||
request, _ := http.NewRequest("GET", url, nil) | ||
request.Header.Set("Accept", "application/json") | ||
type pypiPackageDataFactory struct { | ||
client *helper.Client | ||
} | ||
|
||
client := &http.Client{} | ||
response, err := client.Do(request) | ||
if err != nil { | ||
return nil, err | ||
} | ||
type PypiPackageDataFactory interface { | ||
GetPackageData(packageJSONURL string) (PypiPackageData, error) | ||
Comment on lines
+88
to
+93
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm trying to understand why we need a new interface type. Could you explain? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The interface was introduced so that all receiver functions could be mocked in tests. |
||
GetMaintainerData(pkgData PypiPackageData) (string, string) | ||
GetChecksum(pkgData PypiPackageData, metadata Metadata) *meta.Checksum | ||
GetDownloadLocationFromPyPiPackageData(pkgData PypiPackageData, metadata Metadata) string | ||
} | ||
|
||
if response.StatusCode != http.StatusOK { | ||
return nil, errorPypiCouldNotFetchPkgData | ||
// NewPypiPackageDataFactory ... | ||
func NewPypiPackageDataFactory(client *helper.Client) PypiPackageDataFactory { | ||
return &pypiPackageDataFactory{ | ||
client: client, | ||
} | ||
|
||
return response, err | ||
} | ||
|
||
func GetPackageDataFromPyPi(packageJSONURL string) (PypiPackageData, error) { | ||
func (pf *pypiPackageDataFactory) GetPackageData(packageJSONURL string) (PypiPackageData, error) { | ||
packageInfo := PypiPackageData{} | ||
|
||
response, err := makeGetRequest(packageJSONURL) | ||
packageJSONURL = strings.Replace(packageJSONURL, "pypi.org", "", 1) | ||
response, err := pf.client.HTTP.Get(fmt.Sprintf("%s%s", pf.client.BaseURL, packageJSONURL)) | ||
if err != nil { | ||
return packageInfo, err | ||
} | ||
|
||
if response.StatusCode != http.StatusOK { | ||
return packageInfo, errorPypiCouldNotFetchPkgData | ||
} | ||
defer response.Body.Close() | ||
|
||
jsondata, _ := io.ReadAll(response.Body) | ||
|
@@ -119,7 +125,7 @@ func GetPackageDataFromPyPi(packageJSONURL string) (PypiPackageData, error) { | |
return packageInfo, nil | ||
} | ||
|
||
func GetMaintenerDataFromPyPiPackageData(pkgData PypiPackageData) (string, string) { | ||
func (pf *pypiPackageDataFactory) GetMaintainerData(pkgData PypiPackageData) (string, string) { | ||
var name string | ||
var email string | ||
if len(pkgData.Info.Maintainer) > 0 { | ||
|
@@ -131,27 +137,54 @@ func GetMaintenerDataFromPyPiPackageData(pkgData PypiPackageData) (string, strin | |
return name, email | ||
} | ||
|
||
func GetHighestOrderHashData(digests DigestTypes) (meta.HashAlgorithm, string) { | ||
var algoType meta.HashAlgorithm | ||
var digestValue string | ||
func (pf *pypiPackageDataFactory) GetChecksum(pkgData PypiPackageData, metadata Metadata) *meta.Checksum { | ||
checksum := meta.Checksum{ | ||
Algorithm: meta.HashAlgoSHA1, | ||
Content: []byte(pkgData.Info.Name), | ||
} | ||
|
||
v := reflect.ValueOf(digests) | ||
for _, algo := range HashAlgoPickOrder { | ||
f := v.FieldByName(string(algo)) | ||
if f.IsValid() { | ||
algoType = algo | ||
digestValue = f.String() | ||
return algoType, digestValue | ||
for _, packageDistInfo := range pkgData.Urls { | ||
distInfo, status := getPackageBDistWheelInfo(packageDistInfo, metadata.Generator, metadata.Tag, metadata.CPVersion) | ||
if status { | ||
algo, value := getHighestOrderHashData(distInfo.Digests) | ||
checksum.Algorithm = algo | ||
checksum.Value = value | ||
return &checksum | ||
} | ||
|
||
distInfo, status = getPackageSDistInfo(packageDistInfo, "sdist") | ||
if status { | ||
algo, value := getHighestOrderHashData(distInfo.Digests) | ||
checksum.Algorithm = algo | ||
checksum.Value = value | ||
return &checksum | ||
} | ||
} | ||
|
||
return algoType, digestValue | ||
return &checksum | ||
} | ||
|
||
func (pf *pypiPackageDataFactory) GetDownloadLocationFromPyPiPackageData(pkgData PypiPackageData, metadata Metadata) string { | ||
for _, packageDistInfo := range pkgData.Urls { | ||
distInfo, status := getPackageBDistWheelInfo(packageDistInfo, metadata.Generator, metadata.Tag, metadata.CPVersion) | ||
if status { | ||
return distInfo.URL | ||
} | ||
|
||
distInfo, status = getPackageSDistInfo(packageDistInfo, "sdist") | ||
if status { | ||
return distInfo.URL | ||
} | ||
} | ||
|
||
return "" | ||
} | ||
|
||
func GetPackageBDistWheelInfo(distInfo PypiPackageDistInfo, generator string, tag string, cpversion string) (PypiPackageDistInfo, bool) { | ||
func getPackageBDistWheelInfo(distInfo PypiPackageDistInfo, generator string, | ||
tag string, cpVersion string) (PypiPackageDistInfo, bool) { | ||
PackageType := strings.EqualFold(distInfo.PackageType, generator) | ||
Tag := strings.Contains(strings.ToLower(distInfo.Filename), strings.ToLower(tag)) | ||
CPVersion := strings.EqualFold(distInfo.PythonVersion, cpversion) | ||
CPVersion := strings.EqualFold(distInfo.PythonVersion, cpVersion) | ||
Py2Py3 := strings.Contains(strings.ToLower("py2.py3"), strings.ToLower(distInfo.PythonVersion)) | ||
|
||
status := false | ||
|
@@ -163,7 +196,7 @@ func GetPackageBDistWheelInfo(distInfo PypiPackageDistInfo, generator string, ta | |
return distInfo, status | ||
} | ||
|
||
func GetPackageSDistInfo(distInfo PypiPackageDistInfo, generator string) (PypiPackageDistInfo, bool) { | ||
func getPackageSDistInfo(distInfo PypiPackageDistInfo, generator string) (PypiPackageDistInfo, bool) { | ||
PackageType := strings.EqualFold(distInfo.PackageType, generator) | ||
Source := strings.EqualFold(distInfo.PythonVersion, "source") | ||
|
||
|
@@ -176,45 +209,19 @@ func GetPackageSDistInfo(distInfo PypiPackageDistInfo, generator string) (PypiPa | |
return distInfo, status | ||
} | ||
|
||
func GetChecksumeFromPyPiPackageData(pkgData PypiPackageData, metadata Metadata) *meta.Checksum { | ||
checksum := meta.Checksum{ | ||
Algorithm: meta.HashAlgoSHA1, | ||
Content: []byte(pkgData.Info.Name), | ||
} | ||
|
||
for _, packageDistInfo := range pkgData.Urls { | ||
distInfo, status := GetPackageBDistWheelInfo(packageDistInfo, metadata.Generator, metadata.Tag, metadata.CPVersion) | ||
if status { | ||
algo, value := GetHighestOrderHashData(distInfo.Digests) | ||
checksum.Algorithm = algo | ||
checksum.Value = value | ||
return &checksum | ||
} | ||
|
||
distInfo, status = GetPackageSDistInfo(packageDistInfo, "sdist") | ||
if status { | ||
algo, value := GetHighestOrderHashData(distInfo.Digests) | ||
checksum.Algorithm = algo | ||
checksum.Value = value | ||
return &checksum | ||
} | ||
} | ||
|
||
return &checksum | ||
} | ||
|
||
func GetDownloadLocationFromPyPiPackageData(pkgData PypiPackageData, metadata Metadata) string { | ||
for _, packageDistInfo := range pkgData.Urls { | ||
distInfo, status := GetPackageBDistWheelInfo(packageDistInfo, metadata.Generator, metadata.Tag, metadata.CPVersion) | ||
if status { | ||
return distInfo.URL | ||
} | ||
func getHighestOrderHashData(digests DigestTypes) (meta.HashAlgorithm, string) { | ||
var algoType meta.HashAlgorithm | ||
var digestValue string | ||
|
||
distInfo, status = GetPackageSDistInfo(packageDistInfo, "sdist") | ||
if status { | ||
return distInfo.URL | ||
v := reflect.ValueOf(digests) | ||
for _, algo := range HashAlgoPickOrder { | ||
f := v.FieldByName(string(algo)) | ||
if f.IsValid() { | ||
algoType = algo | ||
digestValue = f.String() | ||
return algoType, digestValue | ||
} | ||
} | ||
|
||
return "" | ||
return algoType, digestValue | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package worker | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"os" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/opensbom-generator/parsers/internal/helper" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestGetPackageDataFromPyPi(t *testing.T) { | ||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
switch strings.TrimSpace(r.URL.Path) { | ||
case "/pypi/requests/jso": | ||
byteData, err := os.ReadFile("../testdata/requests_pypi_data.json") | ||
if err != nil { | ||
panic(err) | ||
} | ||
w.Header().Set("Content-Type", "application/json") | ||
w.WriteHeader(http.StatusOK) | ||
w.Write(byteData) | ||
default: | ||
http.NotFoundHandler().ServeHTTP(w, r) | ||
} | ||
})) | ||
defer server.Close() | ||
|
||
for name, tc := range map[string]struct { | ||
packageJSONUrl string | ||
expectedErr error | ||
}{ | ||
"valid package url": { | ||
packageJSONUrl: "/pypi/requests/jso", | ||
expectedErr: nil, | ||
}, | ||
} { | ||
t.Run(name, func(t *testing.T) { | ||
mockClient := helper.NewClient(server.URL) | ||
factory := NewPypiPackageDataFactory(mockClient) | ||
packageInfo, err := factory.GetPackageData(tc.packageJSONUrl) | ||
require.ErrorIs(t, tc.expectedErr, err) | ||
require.NotNil(t, packageInfo) | ||
}) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is good for now. Thanks!