Skip to content
quarry Artifact [8bd2607aa8]
Login

Artifact [8bd2607aa8]

Artifact 8bd2607aa8a52187c99e9977d0e9d712e0821e960edf998ab30024a6e1f298f1:


package oltp

import (
	"errors"
	"slices"
	"testing"
)

// A token's repositories change in place: the same row and value reach
// the new list, a revoked token cannot be changed, and a site token is
// refused a list (#411).
func TestATokensRepositoriesChangeInPlace(t *testing.T) {
	db := agentRig(t)
	value, tok, err := db.MintToken("dmestas-coder", TokenSpec{Name: "t", Scopes: []string{ScopeSync, ScopeAPI}, Repos: []string{"context"}})
	if err != nil {
		t.Fatal(err)
	}
	got, err := db.SetTokenRepos("dmestas-coder", tok.ID, []string{"context", "kb", "kb"})
	if err != nil {
		t.Fatal(err)
	}
	if got.ID != tok.ID || !slices.Equal(got.Repos, []string{"context", "kb"}) || !got.Reaches("kb") || got.Reaches("other") {
		t.Errorf("FAIL: after the change the token is %+v", got)
	}
	if again, ok := db.LiveToken("dmestas-coder", tok.ID); !ok || !again.Reaches("kb") {
		t.Errorf("FAIL: the change did not stick")
	}
	if value == "" {
		t.Fatal("no value minted")
	}
	if err := db.RevokeToken("dmestas-coder", tok.ID); err != nil {
		t.Fatal(err)
	}
	if _, err := db.SetTokenRepos("dmestas-coder", tok.ID, []string{"kb"}); err == nil {
		t.Errorf("FAIL: a revoked token was changed")
	}
	if _, err := db.SetTokenRepos("dmestas-coder", "nope", nil); err == nil {
		t.Errorf("FAIL: an unknown token was changed")
	}
	var r Refusal
	if errors.As(err, &r) {
		t.Errorf("FAIL: an unknown token read as a refusal")
	}
}