env CUE_CONFIG_DIR=$WORK/cueconfig
# When using the --token flag, we should not need to interact with a registry
# to store the token into logins.json.
oauthregistry device-code-expired

# Unrecognized token formats fail for now.
! exec cue login --token=unrecognized_token_format_1234
stderr -count=1 'unknown token format, expected a prefix like appv1_'

# An empty token is rejected, to prevent issues like --token=${UNSET_VAR}.
! exec cue login --token=
stderr -count=1 'the --token flag needs a non-empty string'

# Ensure that only one token is stored when starting from an empty logins.json file.
exec cue login --token=appv1_validtoken1234
grep -count=1 '"registries": {' cueconfig/logins.json
grep -count=1 '"access_token"' cueconfig/logins.json

# Ensure the contents of the token look correct.
grep -count=1 '"access_token": "appv1_validtoken1234"' cueconfig/logins.json
# appv1 tokens do not include any other field.
! grep '"token_type"' cueconfig/logins.json
! grep '"expiry"' cueconfig/logins.json
! grep '"refresh_token"' cueconfig/logins.json

# Ensure that we can store a token for a new registry on top of an existing logins.json file.
exec cue login --token=appv1_validtoken5678 registry.mycorp.tld
grep -count=1 '"registries": {' cueconfig/logins.json
grep -count=1 '"registry\.mycorp\.tld"' cueconfig/logins.json
grep -count=2 '"access_token"' cueconfig/logins.json

# Ensure forward compatibility.
exec cue login --token=nextv101_validtoken1234 registry.mycorp.tld
grep -count=1 '"nextv101_validtoken1234"' cueconfig/logins.json

# The full CUE_REGISTRY "simple" syntax is allowed.
exec cue login --token=appv1_validtoken9999 fullsyntaxregistry.mycorp.tld/foobar+insecure
grep -count=1 '"fullsyntaxregistry\.mycorp\.tld"' cueconfig/logins.json

# When multiple registries are configured, none is selected by default.
! exec cue login --token=appv1_validtoken_neverused mycorp.tld=registry.mycorp.tld,registry.cue.works
stderr 'need a single CUE registry to log into'

env CUE_REGISTRY=mycorp.tld=registry.mycorp.tld,registry.cue.works
! exec cue login --token=appv1_validtoken_neverused
stderr 'need a single CUE registry to log into'

! grep 'validtoken_neverused' cueconfig/logins.json
