github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/packages/npm/creator_test.go (about)

     1  // Copyright 2023 The GitBundle Inc. All rights reserved.
     2  // Copyright 2017 The Gitea Authors. All rights reserved.
     3  // Use of this source code is governed by a MIT-style
     4  // license that can be found in the LICENSE file.
     5  
     6  package npm
     7  
     8  import (
     9  	"bytes"
    10  	"encoding/base64"
    11  	"fmt"
    12  	"strings"
    13  	"testing"
    14  
    15  	"github.com/gitbundle/modules/json"
    16  
    17  	"github.com/stretchr/testify/assert"
    18  )
    19  
    20  func TestParsePackage(t *testing.T) {
    21  	packageScope := "@scope"
    22  	packageName := "test-package"
    23  	packageFullName := packageScope + "/" + packageName
    24  	packageVersion := "1.0.1-pre"
    25  	packageTag := "latest"
    26  	packageAuthor := "KN4CK3R"
    27  	packageDescription := "Test Description"
    28  	data := "H4sIAAAAAAAA/ytITM5OTE/VL4DQelnF+XkMVAYGBgZmJiYK2MRBwNDcSIHB2NTMwNDQzMwAqA7IMDUxA9LUdgg2UFpcklgEdAql5kD8ogCnhwio5lJQUMpLzE1VslJQcihOzi9I1S9JLS7RhSYIJR2QgrLUouLM/DyQGkM9Az1D3YIiqExKanFyUWZBCVQ2BKhVwQVJDKwosbQkI78IJO/tZ+LsbRykxFXLNdA+HwWjYBSMgpENACgAbtAACAAA"
    29  	integrity := "sha512-yA4FJsVhetynGfOC1jFf79BuS+jrHbm0fhh+aHzCQkOaOBXKf9oBnC4a6DnLLnEsHQDRLYd00cwj8sCXpC+wIg=="
    30  
    31  	t.Run("InvalidUpload", func(t *testing.T) {
    32  		p, err := ParsePackage(bytes.NewReader([]byte{0}))
    33  		assert.Nil(t, p)
    34  		assert.Error(t, err)
    35  	})
    36  
    37  	t.Run("InvalidUploadNoData", func(t *testing.T) {
    38  		b, _ := json.Marshal(packageUpload{})
    39  		p, err := ParsePackage(bytes.NewReader(b))
    40  		assert.Nil(t, p)
    41  		assert.ErrorIs(t, err, ErrInvalidPackage)
    42  	})
    43  
    44  	t.Run("InvalidPackageName", func(t *testing.T) {
    45  		test := func(t *testing.T, name string) {
    46  			b, _ := json.Marshal(packageUpload{
    47  				PackageMetadata: PackageMetadata{
    48  					ID:   name,
    49  					Name: name,
    50  					Versions: map[string]*PackageMetadataVersion{
    51  						packageVersion: {
    52  							Name: name,
    53  						},
    54  					},
    55  				},
    56  			})
    57  
    58  			p, err := ParsePackage(bytes.NewReader(b))
    59  			assert.Nil(t, p)
    60  			assert.ErrorIs(t, err, ErrInvalidPackageName)
    61  		}
    62  
    63  		test(t, " test ")
    64  		test(t, " test")
    65  		test(t, "test ")
    66  		test(t, "te st")
    67  		test(t, "invalid/scope")
    68  		test(t, "@invalid/_name")
    69  		test(t, "@invalid/.name")
    70  	})
    71  
    72  	t.Run("ValidPackageName", func(t *testing.T) {
    73  		test := func(t *testing.T, name string) {
    74  			b, _ := json.Marshal(packageUpload{
    75  				PackageMetadata: PackageMetadata{
    76  					ID:   name,
    77  					Name: name,
    78  					Versions: map[string]*PackageMetadataVersion{
    79  						packageVersion: {
    80  							Name: name,
    81  						},
    82  					},
    83  				},
    84  			})
    85  
    86  			p, err := ParsePackage(bytes.NewReader(b))
    87  			assert.Nil(t, p)
    88  			assert.ErrorIs(t, err, ErrInvalidPackageVersion)
    89  		}
    90  
    91  		test(t, "test")
    92  		test(t, "@scope/name")
    93  		test(t, packageFullName)
    94  	})
    95  
    96  	t.Run("InvalidPackageVersion", func(t *testing.T) {
    97  		version := "first-version"
    98  		b, _ := json.Marshal(packageUpload{
    99  			PackageMetadata: PackageMetadata{
   100  				ID:   packageFullName,
   101  				Name: packageFullName,
   102  				Versions: map[string]*PackageMetadataVersion{
   103  					version: {
   104  						Name:    packageFullName,
   105  						Version: version,
   106  					},
   107  				},
   108  			},
   109  		})
   110  
   111  		p, err := ParsePackage(bytes.NewReader(b))
   112  		assert.Nil(t, p)
   113  		assert.ErrorIs(t, err, ErrInvalidPackageVersion)
   114  	})
   115  
   116  	t.Run("InvalidAttachment", func(t *testing.T) {
   117  		b, _ := json.Marshal(packageUpload{
   118  			PackageMetadata: PackageMetadata{
   119  				ID:   packageFullName,
   120  				Name: packageFullName,
   121  				Versions: map[string]*PackageMetadataVersion{
   122  					packageVersion: {
   123  						Name:    packageFullName,
   124  						Version: packageVersion,
   125  					},
   126  				},
   127  			},
   128  			Attachments: map[string]*PackageAttachment{
   129  				"dummy.tgz": {},
   130  			},
   131  		})
   132  
   133  		p, err := ParsePackage(bytes.NewReader(b))
   134  		assert.Nil(t, p)
   135  		assert.ErrorIs(t, err, ErrInvalidAttachment)
   136  	})
   137  
   138  	t.Run("InvalidData", func(t *testing.T) {
   139  		filename := fmt.Sprintf("%s-%s.tgz", packageFullName, packageVersion)
   140  		b, _ := json.Marshal(packageUpload{
   141  			PackageMetadata: PackageMetadata{
   142  				ID:   packageFullName,
   143  				Name: packageFullName,
   144  				Versions: map[string]*PackageMetadataVersion{
   145  					packageVersion: {
   146  						Name:    packageFullName,
   147  						Version: packageVersion,
   148  					},
   149  				},
   150  			},
   151  			Attachments: map[string]*PackageAttachment{
   152  				filename: {
   153  					Data: "/",
   154  				},
   155  			},
   156  		})
   157  
   158  		p, err := ParsePackage(bytes.NewReader(b))
   159  		assert.Nil(t, p)
   160  		assert.ErrorIs(t, err, ErrInvalidAttachment)
   161  	})
   162  
   163  	t.Run("InvalidIntegrity", func(t *testing.T) {
   164  		filename := fmt.Sprintf("%s-%s.tgz", packageFullName, packageVersion)
   165  		b, _ := json.Marshal(packageUpload{
   166  			PackageMetadata: PackageMetadata{
   167  				ID:   packageFullName,
   168  				Name: packageFullName,
   169  				Versions: map[string]*PackageMetadataVersion{
   170  					packageVersion: {
   171  						Name:    packageFullName,
   172  						Version: packageVersion,
   173  						Dist: PackageDistribution{
   174  							Integrity: "sha512-test==",
   175  						},
   176  					},
   177  				},
   178  			},
   179  			Attachments: map[string]*PackageAttachment{
   180  				filename: {
   181  					Data: data,
   182  				},
   183  			},
   184  		})
   185  
   186  		p, err := ParsePackage(bytes.NewReader(b))
   187  		assert.Nil(t, p)
   188  		assert.ErrorIs(t, err, ErrInvalidIntegrity)
   189  	})
   190  
   191  	t.Run("InvalidIntegrity2", func(t *testing.T) {
   192  		filename := fmt.Sprintf("%s-%s.tgz", packageFullName, packageVersion)
   193  		b, _ := json.Marshal(packageUpload{
   194  			PackageMetadata: PackageMetadata{
   195  				ID:   packageFullName,
   196  				Name: packageFullName,
   197  				Versions: map[string]*PackageMetadataVersion{
   198  					packageVersion: {
   199  						Name:    packageFullName,
   200  						Version: packageVersion,
   201  						Dist: PackageDistribution{
   202  							Integrity: integrity,
   203  						},
   204  					},
   205  				},
   206  			},
   207  			Attachments: map[string]*PackageAttachment{
   208  				filename: {
   209  					Data: base64.StdEncoding.EncodeToString([]byte("data")),
   210  				},
   211  			},
   212  		})
   213  
   214  		p, err := ParsePackage(bytes.NewReader(b))
   215  		assert.Nil(t, p)
   216  		assert.ErrorIs(t, err, ErrInvalidIntegrity)
   217  	})
   218  
   219  	t.Run("Valid", func(t *testing.T) {
   220  		filename := fmt.Sprintf("%s-%s.tgz", packageFullName, packageVersion)
   221  		b, _ := json.Marshal(packageUpload{
   222  			PackageMetadata: PackageMetadata{
   223  				ID:   packageFullName,
   224  				Name: packageFullName,
   225  				DistTags: map[string]string{
   226  					packageTag: packageVersion,
   227  				},
   228  				Versions: map[string]*PackageMetadataVersion{
   229  					packageVersion: {
   230  						Name:        packageFullName,
   231  						Version:     packageVersion,
   232  						Description: packageDescription,
   233  						Author:      User{Name: packageAuthor},
   234  						License:     "MIT",
   235  						Homepage:    "https://gitbundle.com/",
   236  						Readme:      packageDescription,
   237  						Dependencies: map[string]string{
   238  							"package": "1.2.0",
   239  						},
   240  						Dist: PackageDistribution{
   241  							Integrity: integrity,
   242  						},
   243  					},
   244  				},
   245  			},
   246  			Attachments: map[string]*PackageAttachment{
   247  				filename: {
   248  					Data: data,
   249  				},
   250  			},
   251  		})
   252  
   253  		p, err := ParsePackage(bytes.NewReader(b))
   254  		assert.NotNil(t, p)
   255  		assert.NoError(t, err)
   256  
   257  		assert.Equal(t, packageFullName, p.Name)
   258  		assert.Equal(t, packageVersion, p.Version)
   259  		assert.Equal(t, []string{packageTag}, p.DistTags)
   260  		assert.Equal(t, fmt.Sprintf("%s-%s.tgz", strings.Split(packageFullName, "/")[1], packageVersion), p.Filename)
   261  		b, _ = base64.StdEncoding.DecodeString(data)
   262  		assert.Equal(t, b, p.Data)
   263  		assert.Equal(t, packageName, p.Metadata.Name)
   264  		assert.Equal(t, packageScope, p.Metadata.Scope)
   265  		assert.Equal(t, packageDescription, p.Metadata.Description)
   266  		assert.Equal(t, packageDescription, p.Metadata.Readme)
   267  		assert.Equal(t, packageAuthor, p.Metadata.Author)
   268  		assert.Equal(t, "MIT", p.Metadata.License)
   269  		assert.Equal(t, "https://gitbundle.com/", p.Metadata.ProjectURL)
   270  		assert.Contains(t, p.Metadata.Dependencies, "package")
   271  		assert.Equal(t, "1.2.0", p.Metadata.Dependencies["package"])
   272  	})
   273  }