# Test that we can authenticate to a registry with basic auth.

env DOCKER_CONFIG=$WORK/dockerconfig
env-fill $DOCKER_CONFIG/config.json
exec cue export .
cmp stdout expect-stdout

# Sanity-check that we get an error when using the wrong password.
rm $CUE_CACHE_DIR
env-fill dockerconfig/badpassword.json
cp dockerconfig/badpassword.json dockerconfig/config.json
! exec cue export .
stderr 'import failed: cannot find package "example.com/e": cannot fetch example.com/e@v0.0.1: module example.com/e@v0.0.1: 401 Unauthorized: unauthorized: authentication required: invalid user-password credentials'

# Sanity-check that a configured default helper which is not installed
# is not treated as a fatal error. See https://cuelang.org/issue/2934.
rm $CUE_CACHE_DIR
env-fill dockerconfig/missing_default_helper.json
cp dockerconfig/missing_default_helper.json dockerconfig/config.json
exec cue export .
cmp stdout expect-stdout
-- dockerconfig/config.json --
{
	"auths": {
		"${DEBUG_REGISTRY_HOST}": {
			"username": "someone",
			"password": "something"
		}
	}
}
-- dockerconfig/badpassword.json --
{
	"auths": {
		"${DEBUG_REGISTRY_HOST}": {
			"username": "someone",
			"password": "wrongpassword"
		}
	}
}
-- dockerconfig/missing_default_helper.json --
{
	"credsStore": "definitely-not-found-executable",
	"auths": {
		"${DEBUG_REGISTRY_HOST}": {
			"username": "someone",
			"password": "something"
		}
	}
}
-- expect-stdout --
"ok"
-- main.cue --
package main
import "example.com/e"

e.foo

-- cue.mod/module.cue --
module: "test.org"
language: version: "v0.8.0"
deps: "example.com/e": v: "v0.0.1"
-- _registry/auth.json --
{"username": "someone", "password": "something"}
-- _registry_prefix --
somewhere/other
-- _registry/example.com_e_v0.0.1/cue.mod/module.cue --
module: "example.com/e@v0"
language: version: "v0.8.0"

-- _registry/example.com_e_v0.0.1/main.cue --
package e

foo: "ok"

