diff --git a/pkg/npm/plugin_impl.go b/pkg/npm/plugin_impl.go index c5be953..7e248bd 100644 --- a/pkg/npm/plugin_impl.go +++ b/pkg/npm/plugin_impl.go @@ -5,6 +5,13 @@ package npm +import ( + "encoding/base64" + "fmt" + "net/url" + "strings" +) + // Settings for the Plugin. type Settings struct { Username string @@ -18,6 +25,9 @@ type Settings struct { Access string } +// globalRegistry defines the default NPM registry. +const globalRegistry = "https://registry.npmjs.org" + func (p *pluginImpl) Validate() error { // Validate the Config and return an error if there are issues. return nil @@ -27,3 +37,26 @@ func (p *pluginImpl) Exec() error { // Implementation of the plugin. return nil } + +// npmrcContentsUsernamePassword creates the contents from a username and +// password +func npmrcContentsUsernamePassword(config Settings) string { + // get the base64 encoded string + authString := fmt.Sprintf("%s:%s", config.Username, config.Password) + encoded := base64.StdEncoding.EncodeToString([]byte(authString)) + + // create the file contents + return fmt.Sprintf("_auth = %s\nemail = %s", encoded, config.Email) +} + +/// Writes npmrc contents when using a token +func npmrcContentsToken(config Settings) string { + registry, _ := url.Parse(config.Registry) + registry.Scheme = "" // Reset the scheme to empty. This makes it so we will get a protocol relative URL. + registryString := registry.String() + + if !strings.HasSuffix(registryString, "/") { + registryString = registryString + "/" + } + return fmt.Sprintf("%s:_authToken=%s", registryString, config.Token) +} diff --git a/pkg/npm/plugin_test.go b/pkg/npm/plugin_test.go index dffdf4a..30e1a22 100644 --- a/pkg/npm/plugin_test.go +++ b/pkg/npm/plugin_test.go @@ -7,6 +7,42 @@ package npm import "testing" -func TestPlugin(t *testing.T) { - t.Skip() +func TestTokenRCContents(t *testing.T) { + settings := Settings{ + Registry: "https://npm.someorg.com/", + Token: "token", + } + actual := npmrcContentsToken(settings) + expected := "//npm.someorg.com/:_authToken=token" + if actual != expected { + t.Errorf("Unexpected token settings (Got: %s, Expected: %s)", actual, expected) + } + + settings.Registry = "https://npm.someorg.com/with/path/" + actual = npmrcContentsToken(settings) + expected = "//npm.someorg.com/with/path/:_authToken=token" + if actual != expected { + t.Errorf("Unexpected token settings (Got: %s, Expected: %s)", actual, expected) + } + + settings.Registry = globalRegistry + actual = npmrcContentsToken(settings) + expected = "//registry.npmjs.org/:_authToken=token" + if actual != expected { + t.Errorf("Unexpected token settings (Got: %s, Expected: %s)", actual, expected) + } + + settings.Registry = "https://npm.someorg.com" + actual = npmrcContentsToken(settings) + expected = "//npm.someorg.com/:_authToken=token" + if actual != expected { + t.Errorf("Unexpected token settings (Got: %s, Expected: %s)", actual, expected) + } + + settings.Registry = "https://npm.someorg.com/with/path" + actual = npmrcContentsToken(settings) + expected = "//npm.someorg.com/with/path/:_authToken=token" + if actual != expected { + t.Errorf("Unexpected token settings (Got: %s, Expected: %s)", actual, expected) + } } diff --git a/plugin_test.go b/plugin_test.go deleted file mode 100644 index ac6f6be..0000000 --- a/plugin_test.go +++ /dev/null @@ -1,45 +0,0 @@ -package main - -import ( - "testing" -) - -func TestTokenRCContents(t *testing.T) { - conf := Config{ - Registry: "https://npm.someorg.com/", - Token: "token", - } - actual := npmrcContentsToken(conf) - expected := "//npm.someorg.com/:_authToken=token" - if actual != expected { - t.Errorf("Unexpected token config (Got: %s, Expected: %s)", actual, expected) - } - - conf.Registry = "https://npm.someorg.com/with/path/" - actual = npmrcContentsToken(conf) - expected = "//npm.someorg.com/with/path/:_authToken=token" - if actual != expected { - t.Errorf("Unexpected token config (Got: %s, Expected: %s)", actual, expected) - } - - conf.Registry = GlobalRegistry - actual = npmrcContentsToken(conf) - expected = "//registry.npmjs.org/:_authToken=token" - if actual != expected { - t.Errorf("Unexpected token config (Got: %s, Expected: %s)", actual, expected) - } - - conf.Registry = "https://npm.someorg.com" - actual = npmrcContentsToken(conf) - expected = "//npm.someorg.com/:_authToken=token" - if actual != expected { - t.Errorf("Unexpected token config (Got: %s, Expected: %s)", actual, expected) - } - - conf.Registry = "https://npm.someorg.com/with/path" - actual = npmrcContentsToken(conf) - expected = "//npm.someorg.com/with/path/:_authToken=token" - if actual != expected { - t.Errorf("Unexpected token config (Got: %s, Expected: %s)", actual, expected) - } -}