Documentation
¶
Overview ¶
Package githubtest is a fake GitHub for tests of code that reads and writes repos: the mounts GitHub driver, the storage example app, and consumers' own tests. It serves the REST routes the driver uses (a repo, a commit, a branch's commits, the tags, a tarball, and the Contents API) and the GraphQL tags query over repos held in memory, and each write makes a real commit on its branch. It serves the hooks API too, and Push delivers a signed push to each hook made through it. PushRequest and SignedRequest build signed webhook deliveries for an app's handler.
A private repo answers only a request with an accepted token, as GitHub does, with 404 for anything else. Once a private repo has grants (Repo.Grant), only a granted token reads it. Every request is recorded with the token it carried, so a test can check that a connection's token reached each one.
It serves the Git LFS batch API for downloads (lfs.go), so a repo can hold LFS files, whose tarball, raw and contents answers carry only the pointer, as GitHub's do.
It serves a GitHub App's sign-in on the same host, at the paths oneauth's GithubOAuth2.SetEnterpriseURL points at: the web flow with PKCE, the device flow, refresh, /user and org membership. AddApp registers the App, AddUser makes a person, and SignInAs or User.Approve stands in for the person in the browser. Each sign-in issues the person a token, which Repo.GrantUser and Repo.HookManagerUsers name by login. Every API route also answers under /api/v3, as on a GitHub Enterprise Server host.
oneauth's outbound/githubtest mints installation tokens and user tokens for a connection, with no sign-in or repos. A test that drives a connection end to end uses both, with AcceptToken trusting the tokens the other mints.
Index ¶
- func LFSPointer(content string) (pointer, oid string)
- func SignedRequest(target string, secret []byte, event string, body []byte) *http.Request
- type Hook
- type Option
- type Repo
- func (r *Repo) AddTarEntries(sha string, es ...TarEntry)
- func (r *Repo) Commit(branch string, put map[string]string, del []string) string
- func (r *Repo) CommitLFS(branch, p, content string) (sha, oid string)
- func (r *Repo) File(branch, p string) (string, bool)
- func (r *Repo) Grant(tokens ...string)
- func (r *Repo) GrantUser(logins ...string)
- func (r *Repo) Head(branch string) string
- func (r *Repo) HookManagerUsers(logins ...string)
- func (r *Repo) HookManagers(tokens ...string)
- func (r *Repo) Hooks() []Hook
- func (r *Repo) Push(branch string, put map[string]string, del []string) (string, error)
- func (r *Repo) PushRequest(target string, secret []byte, branch string) *http.Request
- func (r *Repo) SetCommitTime(sha string, t time.Time)
- func (r *Repo) Source() string
- func (r *Repo) Symlink(branch, p, target string) string
- func (r *Repo) Tag(name, sha string, annotated bool)
- type Request
- type Server
- func (s *Server) AcceptToken(f func(token string) bool)
- func (s *Server) AddApp(clientID, clientSecret string)
- func (s *Server) AddRepo(owner, name string, private bool, files map[string]string) *Repo
- func (s *Server) AddToken(tok string)
- func (s *Server) AddUser(login string, orgs ...string) *User
- func (s *Server) CorruptLFS(on bool)
- func (s *Server) CorruptRaw(on bool)
- func (s *Server) ExpireTokens(login string)
- func (s *Server) FailLFS(status int, message string)
- func (s *Server) FailNext(status int)
- func (s *Server) LFSURL() string
- func (s *Server) ListenURL() string
- func (s *Server) RawURL() string
- func (s *Server) RemoveLFSObject(oid string)
- func (s *Server) Requests() []Request
- func (s *Server) SetDeviceInterval(d time.Duration)
- func (s *Server) SignInAs(login string)
- type TarEntry
- type User
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func LFSPointer ¶ added in v0.7.26
LFSPointer returns the Git LFS pointer file for content, and its oid.
func SignedRequest ¶ added in v0.7.16
SignedRequest builds a webhook delivery to target as GitHub sends one: a POST of body with the event name in X-GitHub-Event and X-Hub-Signature-256 set to the HMAC-SHA256 of body under secret. Serve it to an app's handler with httptest.NewRecorder, or send it with http.DefaultClient.
Types ¶
type Hook ¶ added in v0.7.18
type Hook struct {
ID int64
URL string
ContentType string
Secret string
Events []string
Active bool
Token string
}
Hook is a webhook made on a repo through the hooks API. Token is the token that last made or changed it, so a test can check that the app used the right connection.
type Option ¶ added in v0.7.28
type Option func(*options)
Option configures a fake that New starts.
func WithBaseURL ¶ added in v0.7.28
WithBaseURL sets the origin the fake puts in every URL it hands out: Server.URL, RawURL, LFSURL, the LFS batch hrefs, pagination Link headers and the device flow's verification_uri. Use an address the app under test can reach, such as "http://host.docker.internal:<port>". u is a scheme, a host and an optional port. New fails the test if u has a path, query or fragment. The test process itself may not resolve u, so it reaches the fake through ListenURL.
func WithListener ¶ added in v0.7.28
WithListener serves the fake on l instead of a fresh 127.0.0.1 port. A test whose app runs in a container listens on "0.0.0.0:0" or the docker bridge's gateway, so the container can reach it. The fake closes l when the test ends.
type Repo ¶
type Repo struct {
Owner string
Name string
Private bool
// contains filtered or unexported fields
}
Repo is one repository in the fake. Its methods are safe to call while the server runs.
func (*Repo) AddTarEntries ¶
AddTarEntries adds entries to the tarball of commit sha only. The Contents API doesn't see them.
func (*Repo) Commit ¶
Commit makes a commit on branch, creating the branch if needed. It starts from the branch's current files, sets each of put, and removes each of del. It returns the new commit's sha.
func (*Repo) CommitLFS ¶ added in v0.7.26
CommitLFS commits p on branch as a Git LFS pointer to content, and keeps content as its LFS object. It returns the commit and the object's oid.
func (*Repo) Grant ¶ added in v0.7.17
Grant lets each of tokens read the repo, and accepts each one as AddToken does. Once a private repo has a grant, it answers only granted tokens, and gives any other token 404, accepted or not, as GitHub does for a repo the token can't see. A private repo with no grants answers any accepted token. A public repo answers everyone either way.
func (*Repo) GrantUser ¶ added in v0.7.19
GrantUser lets each of logins read the repo through any token they get by signing in, so the grant holds across refreshes. It limits a private repo as Grant does, and the two add up: a repo with grants of either kind answers a granted token or a granted person's token.
func (*Repo) HookManagerUsers ¶ added in v0.7.19
HookManagerUsers lets each of logins manage the repo's hooks through any token they get by signing in. It limits who may manage hooks as HookManagers does, and the two add up.
func (*Repo) HookManagers ¶ added in v0.7.18
HookManagers limits who may manage the repo's hooks to tokens, and accepts each one as AddToken does. Any other token gets 404 from the hooks API, as GitHub answers a token without admin rights on the repo. With no managers, any accepted token that can see the repo manages its hooks.
func (*Repo) Push ¶ added in v0.7.18
Push commits put and del on branch, as Commit does, then delivers a signed push event to each active hook that wants push events, one at a time, before it returns. GitHub delivers later and in the background; delivering first lets a test check the app's state right after Push. Each delivery is JSON, whatever the hook's content type. Push returns the new commit, and the first delivery that fails or answers other than 2xx.
func (*Repo) PushRequest ¶ added in v0.7.16
PushRequest builds a signed push event to target for branch's head, with its parent as before, as GitHub sends after a push of one commit. It panics on a missing branch.
func (*Repo) SetCommitTime ¶ added in v0.7.16
SetCommitTime sets commit sha's date, so a test can make the order of tags by date differ from their order by name. It panics on a sha the repo doesn't have.
type Request ¶
type Request struct {
Method string
Path string
// Token is the bearer or "token" credential the request carried, or "" for none.
Token string
}
Request is one request the fake answered.
type Server ¶
Server is the fake. Its URL is the API base a driver is pointed at, and the origin of every URL the fake hands out.
func New ¶
New starts a fake and stops it when the test ends. With no options it listens on a fresh 127.0.0.1 port, and URL is that address.
func (*Server) AcceptToken ¶
AcceptToken also accepts any token f approves, such as one oneauth's fake minted (its ValidInstallationToken or ValidUserToken).
func (*Server) AddApp ¶ added in v0.7.19
AddApp registers a GitHub App's OAuth client. The sign-in endpoints answer only a registered client, and give any other client id, or a wrong secret, GitHub's incorrect_client_credentials error.
func (*Server) AddRepo ¶
AddRepo creates owner/name with one commit on branch "main" holding files, and returns it.
func (*Server) AddUser ¶ added in v0.7.19
AddUser makes a person who can sign in, a member of each of orgs. Each sign-in issues the person a new access token and refresh token, and the fake accepts the access token as AddToken does, until ExpireTokens or a refresh retires it. Repo.GrantUser and Repo.HookManagerUsers name the person, so a grant holds across refreshes.
func (*Server) CorruptLFS ¶ added in v0.7.26
CorruptLFS makes each object download with one byte added, so a test can check that a driver refuses bytes that don't match the pointer.
func (*Server) CorruptRaw ¶ added in v0.7.14
CorruptRaw makes the raw host and the git blobs endpoint send each file with one byte added, so a test can check that a driver refuses bytes that don't match their git blob id.
func (*Server) ExpireTokens ¶ added in v0.7.19
ExpireTokens ends every access token issued to login, so each one gets 401 Bad credentials, as GitHub answers an expired token. The person's refresh tokens still work, so a test can watch the app refresh. A token given to AddToken or Grant is unaffected.
func (*Server) FailLFS ¶ added in v0.7.26
FailLFS makes the next batch request answer with status and message, such as 403 with GitHub's quota message.
func (*Server) FailNext ¶
FailNext makes the next request answer with status and no body, then serve normally. 403 sets x-ratelimit-remaining to 0, as GitHub's rate limit does.
func (*Server) LFSURL ¶ added in v0.7.26
LFSURL is the web host whose "/{owner}/{repo}.git/info/lfs/objects/batch" serves the LFS batch API. Point a driver at it with WithLFSURL.
func (*Server) ListenURL ¶ added in v0.7.28
ListenURL is the address the fake listens on, as "http://host:port". It equals URL unless WithBaseURL set another origin.
func (*Server) RawURL ¶ added in v0.7.14
RawURL is the fake's raw host, which serves a file by owner, repo, commit and path as raw.githubusercontent.com does. Point a driver at it with WithRawURL.
func (*Server) RemoveLFSObject ¶ added in v0.7.26
RemoveLFSObject deletes an object, so the batch API reports it missing.
func (*Server) SetDeviceInterval ¶ added in v0.7.19
SetDeviceInterval sets how often a device-flow client may poll, for device codes issued after the call. GitHub's interval is five seconds. The fake's default is one second, so a test that waits through DeviceAccessToken runs quickly.
func (*Server) SignInAs ¶ added in v0.7.19
SignInAs makes the web flow's authorize step sign in as login, as if that person were signed in to GitHub in the browser and approved the app. Until a test calls it, authorize redirects back with error=access_denied. It panics on a login AddUser didn't make.
type TarEntry ¶
type TarEntry struct {
Name string // relative to the tarball's top folder
Type byte // tar.TypeReg, tar.TypeSymlink, ...
Body string
Linkname string
}
TarEntry is an extra entry a test adds to a commit's tarball, such as a link or a path that climbs out, to check that a driver refuses it.
type User ¶ added in v0.7.19
type User struct {
Login string
ID int64
// Name and Email are what /user answers. AddUser sets Name to Login and leaves Email empty,
// which /user answers as null, as GitHub does for a private email.
Name string
Email string
// contains filtered or unexported fields
}
User is a person who can sign in to the fake. AddUser makes one.