# Test that `cue login` succeeds with the device flow
# when the device login is immediately authorized and successful.

env CUE_CONFIG_DIR=$WORK/cueconfig
oauthregistry immediate-success

exec cue login
stdout 'open:.*user_code=user-code'

# Ensure that only one token is stored.
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": "secret-access-token"' cueconfig/logins.json
grep -count=1 '"token_type": "Bearer"' cueconfig/logins.json
# Timestamps are always stored in UTC and truncated to seconds.
grep '"expiry": "20..-..-..T..:..:..Z"' cueconfig/logins.json
# oauthregistry does not give a refresh token, and we use encoding/json's omitempty.
! grep '"refresh_token"' cueconfig/logins.json
