githubtest

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 24, 2026 License: Apache-2.0 Imports: 23 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package githubtest is a fake GitHub for tests: an httptest server with the parts of the REST API and the browser flows vx uses, plus helpers to create users, organizations and repositories, set permissions, install the App and fire signed webhook deliveries.

The fake serves the browser side at URL (like https://github.com) and the API at APIURL = URL + "/api/v3" (like GitHub Enterprise Server), so tests exercise configurable base URLs.

gh := githubtest.New(t)
creds := gh.NewApp()                  // or the manifest flow
api := gh.Client(t)                   // *github.Client against the fake
alice := gh.AddUser("alice")
gh.AddOrg("acme")
gh.AddRepo("acme/shop", false)
gh.SetPermission("acme/shop", "alice", "write")
inst := gh.Install("acme")            // all repositories of acme
gh.Deliver(t, server.URL+"/hooks/github", "installation", gh.InstallationEvent("created", inst))

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DeliveryIDAt

func DeliveryIDAt(t time.Time) string

DeliveryIDAt returns a delivery id made at t, for tests of stale deliveries.

func NewDeliveryID

func NewDeliveryID() string

NewDeliveryID returns a delivery id like GitHub's: a version 1 UUID, which carries the time it was made (now).

func Sign

func Sign(secret string, body []byte) string

Sign returns the X-Hub-Signature-256 value of body with secret.

Types

type Account

type Account struct {
	ID    int64
	Login string
	Type  string // User or Organization
	Name  string
	Email string // primary verified email; empty: none shared
}

Account is a GitHub user or organization.

type Delivery

type Delivery struct {
	Event     string
	ID        string // X-GitHub-Delivery; empty: a new random id
	Body      []byte
	Signature string // X-Hub-Signature-256; empty: signed with the webhook secret, "-": no header
}

Delivery is one webhook delivery.

type Installation

type Installation struct {
	ID          int64
	Account     string // login
	All         bool   // repository_selection all
	Repos       []int64
	SuspendedAt *time.Time
}

Installation is an installation of the App.

type Repo

type Repo struct {
	ID            int64
	Owner         string
	Name          string
	DefaultBranch string
	Private       bool
}

Repo is a repository on the fake.

func (Repo) FullName

func (r Repo) FullName() string

FullName is owner/name.

type Server

type Server struct {
	// URL is the browser base (WebURL), APIURL the REST API base.
	URL    string
	APIURL string
	// Now is the fake's clock, for token expiry and App JWT checks.
	Now func() time.Time
	// TokenTTL is how long installation tokens live (default one hour).
	TokenTTL time.Duration
	// PageSize is the page size of paginated lists (default 30), so tests
	// see pagination with few repositories.
	PageSize int
	// contains filtered or unexported fields
}

Server is a fake GitHub. Its methods are safe for concurrent use.

func New

func New(t testing.TB) *Server

New starts a fake GitHub, closed when the test ends.

func (*Server) Account

func (s *Server) Account(login string) (Account, bool)

Account returns an account by login.

func (*Server) AddInstallationRepos

func (s *Server) AddInstallationRepos(id int64, repos ...string)

AddInstallationRepos adds repositories to a "selected" installation.

func (*Server) AddOrg

func (s *Server) AddOrg(login string) Account

AddOrg creates an organization.

func (*Server) AddRepo

func (s *Server) AddRepo(fullName string, private bool) Repo

AddRepo creates owner/name (the owner must exist) with default branch main.

func (*Server) AddUser

func (s *Server) AddUser(login string) Account

AddUser creates a user with name = login and email login@example.com.

func (*Server) App

App returns the App's credentials; ok is false before it exists.

func (*Server) Calls

func (s *Server) Calls(pattern string) int

Calls returns how many requests matched "METHOD /path" so far, with numbers and names in the path as sent, e.g. "POST /app/installations/1001/access_tokens". A pattern ending in "*" matches by prefix. API paths are given without the /api/v3 prefix.

func (*Server) Client

func (s *Server) Client(t testing.TB) *github.Client

Client returns a github.Client for the App against the fake, creating the App first if it does not exist yet.

func (*Server) Close

func (s *Server) Close()

Close stops the server early; New already closes it at the end of the test.

func (*Server) Configure

Configure is Prepare plus an App: it creates the App on the fake (if it does not exist) and saves it in svc, so svc.GitHubClient, svc.GitHubApp and webhook verification work.

func (*Server) Connector

func (s *Server) Connector() func(service.GitHubAppCredentials) (service.GitHubAPI, error)

Connector returns a service.GitHubSettings.Connect function that makes clients against the fake.

func (*Server) DeleteInstallation

func (s *Server) DeleteInstallation(id int64)

DeleteInstallation uninstalls the App; its tokens stop working.

func (*Server) DeleteRepo

func (s *Server) DeleteRepo(fullName string) Repo

DeleteRepo deletes a repository and returns what it was.

func (*Server) Deliver

func (s *Server) Deliver(t testing.TB, hookURL, event string, payload any) int

Deliver posts a webhook event signed with the App's webhook secret, with a new delivery id made at s.Now(), and returns the response status code.

func (*Server) DeliverRaw

func (s *Server) DeliverRaw(t testing.TB, hookURL string, d Delivery) int

DeliverRaw posts a delivery as given, for tests of bad signatures and replays, and returns the response status code.

func (*Server) DenyAuthorization

func (s *Server) DenyAuthorization(deny bool)

DenyAuthorization makes the authorize page redirect back with error=access_denied, as when the person cancels.

func (*Server) ExpireTokens

func (s *Server) ExpireTokens()

ExpireTokens makes every installation and user token invalid.

func (*Server) Fail

func (s *Server) Fail(pattern string, status, n int)

Fail makes the next n requests matching pattern (see Calls) fail with status. 403 and 429 come back as rate limits, as GitHub sends them.

func (*Server) Install

func (s *Server) Install(account string, repos ...string) int64

Install installs the App on an account. With no repositories it gets all of the account's repositories, now and later.

func (*Server) Installation

func (s *Server) Installation(id int64) (Installation, bool)

Installation returns an installation.

func (*Server) InstallationEvent

func (s *Server) InstallationEvent(action string, id int64) map[string]any

InstallationEvent is an "installation" payload: created, deleted, suspend, unsuspend, new_permissions_accepted.

func (*Server) InstallationReposEvent

func (s *Server) InstallationReposEvent(action string, id int64, repos ...string) map[string]any

InstallationReposEvent is an "installation_repositories" payload: added or removed, listing repos (full names).

func (*Server) InstallationTokenFor

func (s *Server) InstallationTokenFor(id int64) string

InstallationTokenFor mints an installation token directly.

func (*Server) Manifest

func (s *Server) Manifest() map[string]any

Manifest returns the manifest the App was created from (decoded JSON).

func (*Server) ManifestCode

func (s *Server) ManifestCode(manifestJSON []byte) string

ManifestCode registers a manifest as if a site admin had posted it to GitHub and confirmed, and returns the one-time code GitHub sends back.

func (*Server) MemberEvent

func (s *Server) MemberEvent(installationID int64, action, fullName, login string) map[string]any

MemberEvent is a "member" payload (a repository collaborator): added, removed or edited.

func (*Server) MembershipEvent

func (s *Server) MembershipEvent(installationID int64, action, org, team, login string) map[string]any

MembershipEvent is a "membership" payload (team membership): added or removed.

func (*Server) NewApp

func (s *Server) NewApp() service.GitHubAppCredentials

NewApp creates the App directly, as if the manifest flow had run, and returns its credentials.

func (*Server) OAuthCode

func (s *Server) OAuthCode(login, redirectURI, codeChallenge string) string

OAuthCode mints a sign-in code for login, as GitHub's authorize page would after the person approved. codeChallenge is the PKCE S256 challenge ("" for none); redirectURI must match the exchange's.

func (*Server) OrgMemberEvent

func (s *Server) OrgMemberEvent(installationID int64, action, org, login, role string) map[string]any

OrgMemberEvent is an "organization" payload: member_added, member_removed or member_invited.

func (*Server) Prepare

func (s *Server) Prepare(t testing.TB, svc *service.Service)

Prepare points svc at the fake without creating an App: a random server key, the fake's URLs, Connect, and PublicURL http://vx.test when empty. The manifest flow can then run.

func (*Server) PushEvent

func (s *Server) PushEvent(installationID int64, fullName, ref, before, after string) map[string]any

PushEvent is a "push" payload to ref (e.g. refs/heads/main).

func (*Server) RemoveInstallationRepos

func (s *Server) RemoveInstallationRepos(id int64, repos ...string)

RemoveInstallationRepos removes repositories from an installation (an "all" installation becomes "selected").

func (*Server) RenameAccount

func (s *Server) RenameAccount(login, newLogin string)

RenameAccount changes a user's or organization's login (the id stays).

func (*Server) RenameRepo

func (s *Server) RenameRepo(fullName, newName string) Repo

RenameRepo renames a repository (its id stays).

func (*Server) Repo

func (s *Server) Repo(fullName string) (Repo, bool)

Repo returns a repository by full name.

func (*Server) RepoRenamedEvent

func (s *Server) RepoRenamedEvent(installationID int64, fullName, oldName string) map[string]any

RepoRenamedEvent is a "repository" renamed payload; call it after RenameRepo with the old name.

func (*Server) RepoTransferredEvent

func (s *Server) RepoTransferredEvent(installationID int64, fullName, oldOwner string) map[string]any

RepoTransferredEvent is a "repository" transferred payload; call it after TransferRepo with the old owner.

func (*Server) RepositoryEvent

func (s *Server) RepositoryEvent(action string, installationID int64, r Repo) map[string]any

RepositoryEvent is a "repository" payload for r (from Repo, or the value DeleteRepo returned): deleted, privatized, publicized, edited, archived.

func (*Server) SetDefaultBranch

func (s *Server) SetDefaultBranch(fullName, branch string)

SetDefaultBranch changes a repository's default branch.

func (*Server) SetOrgMember

func (s *Server) SetOrgMember(org, login, role string)

SetOrgMember sets login's role in an organization: admin, member, or "" to remove them.

func (*Server) SetPermission

func (s *Server) SetPermission(fullName, login, perm string)

SetPermission sets login's permission on a repository: admin, maintain, write, triage, read, or none/"" to remove it.

func (*Server) SetPrivate

func (s *Server) SetPrivate(fullName string, private bool) Repo

SetPrivate changes a repository's visibility.

func (*Server) SetProfile

func (s *Server) SetProfile(login, name, email string)

SetProfile sets a user's name and primary verified email ("" for none).

func (*Server) SetupClient

func (s *Server) SetupClient(t testing.TB) *github.Client

SetupClient returns a client without App credentials, which can only complete the manifest flow.

func (*Server) Sign

func (s *Server) Sign(body []byte) string

Sign returns the X-Hub-Signature-256 value of body with the App's webhook secret.

func (*Server) SignIn

func (s *Server) SignIn(login string)

SignIn makes login the person signed in at the fake's web pages, so /login/oauth/authorize approves for them right away. "" signs out.

func (*Server) SuspendInstallation

func (s *Server) SuspendInstallation(id int64, suspended bool)

SuspendInstallation suspends or unsuspends an installation.

func (*Server) TeamAddEvent

func (s *Server) TeamAddEvent(installationID int64, org, team, fullName string) map[string]any

TeamAddEvent is a "team_add" payload: a team got access to a repository.

func (*Server) TeamEvent

func (s *Server) TeamEvent(installationID int64, action, org, team, fullName string) map[string]any

TeamEvent is a "team" payload: created, deleted, edited, added_to_repository or removed_from_repository (fullName may be "").

func (*Server) TransferRepo

func (s *Server) TransferRepo(fullName, newOwner string) Repo

TransferRepo moves a repository to another owner (its id stays).

func (*Server) UserToken

func (s *Server) UserToken(login string) string

UserToken mints a user access token for login.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL