client

package
v0.0.0-...-bb2c1c1 Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2022 License: AGPL-3.0 Imports: 20 Imported by: 0

Documentation

Overview

Package client provides a remote.Client implementation that makes requests to cozy-stack. It also provides a fake implementation that mocks it for tests.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Change

type Change struct {
	Seq int
	*remote.ChangedDoc
	Skip bool
}

Change describes an entry in the changes feed of a fake stack/client.

type Client

type Client struct {
	Address      string
	ClientID     string
	ClientSecret string
	AccessToken  string
	RefreshToken string
	Client       *http.Client
}

Client is a client for cozy-stack.

func New

func New(address string) *Client

New returns a client for cozy-stack.

func (*Client) Changes

func (c *Client) Changes(seq *remote.Seq, limit int, skipTrashed bool) (*remote.ChangesResponse, error)

Changes is required by the remote.Client interface.

Note: the design docs are ignored

func (*Client) CreateDir

func (c *Client) CreateDir(parentID remote.ID, name string) (*remote.Doc, error)

CreateDir is required by the remote.Client interface.

func (*Client) DocsByID

func (c *Client) DocsByID() map[remote.ID]*remote.Doc

DocsByID returns a map of id -> doc (for testing purpose).

func (*Client) EmptyTrash

func (c *Client) EmptyTrash() error

EmptyTrash is required by remote.Client interface.

func (*Client) NewRequest

func (c *Client) NewRequest(verb, path string) *request

NewRequest creates a request representation that can be forged to send an HTTP request to the stack.

func (*Client) Refresh

func (c *Client) Refresh() error

Refresh is required by the remote.Client interface.

func (*Client) Register

func (c *Client) Register() error

Register will create an OAuth client on the stack.

func (*Client) Synchronized

func (c *Client) Synchronized() error

Synchronized is required by the remote.Client interface.

func (*Client) Trash

func (c *Client) Trash(doc *remote.Doc) (*remote.Doc, error)

Trash is required by the remote.Client interface.

type Fake

type Fake struct {
	Address   string
	SyncCount int
	ByID      map[remote.ID]*remote.Doc
	Feed      []Change

	// Those functions can be overloaded for some tests where we want to
	// control the values.
	GenerateID   func() remote.ID
	GenerateRev  func(id remote.ID, generation int) remote.Rev
	GenerateSeq  func(generation int) remote.Seq
	ConflictName func(id remote.ID, name string) string
}

Fake can be used to simulate a cozy-stack client (and the stack its-self) for tests.

TODO find a way to simulate latency

func NewFake

func NewFake(address string) *Fake

NewFake creates a fake client that can be used for tests. It doesn't make any HTTP request, it just simulate them via an in-memory mock.

func (*Fake) AddInitialDocs

func (f *Fake) AddInitialDocs(changed ...*remote.ChangedDoc)

AddInitialDocs will create the tree for a new instance (root, trash, etc.).

func (*Fake) Changes

func (f *Fake) Changes(seq *remote.Seq, limit int, skipTrashed bool) (*remote.ChangesResponse, error)

Changes is required by the remote.Client interface.

func (*Fake) CheckInvariants

func (f *Fake) CheckInvariants() error

CheckInvariants checks that we don't have inconsistencies in the fake client. It can be used as a way to detect some bugs in the Fake code.

func (*Fake) CreateDir

func (f *Fake) CreateDir(parentID remote.ID, name string) (*remote.Doc, error)

CreateDir is required by the remote.Client interface.

func (*Fake) DocsByID

func (f *Fake) DocsByID() map[remote.ID]*remote.Doc

DocsByID returns a map of id -> doc (for testing purpose).

func (*Fake) EmptyTrash

func (f *Fake) EmptyTrash() error

EmptyTrash is required by remote.Client interface.

func (*Fake) MatchSequence

func (f *Fake) MatchSequence(seq remote.Seq)

MatchSequence will create dumb entries in the changes feed until it reaches the given sequence generation number. It is used to compensate for the lack of design docs.

func (*Fake) Refresh

func (f *Fake) Refresh() error

Refresh is required by the remote.Client interface.

func (*Fake) Synchronized

func (f *Fake) Synchronized() error

Synchronized is required by the remote.Client interface.

func (*Fake) Trash

func (f *Fake) Trash(doc *remote.Doc) (*remote.Doc, error)

Trash is required by the remote.Client interface.

type Instance

type Instance struct {
	Name  string
	Stack *Stack
}

Instance describes a cozy instance on a stack.

func (*Instance) Address

func (inst *Instance) Address() string

Address return the http address that can be used to access this instance.

func (*Instance) CreateAccessToken

func (inst *Instance) CreateAccessToken(client *Client) error

CreateAccessToken runs a cozy-stack command to create an access token for the given client (admin endpoint, no user interaction).

func (*Instance) Domain

func (inst *Instance) Domain() string

Domain returns the main domain of the instance.

func (*Instance) Remove

func (inst *Instance) Remove() error

Remove runs a cozy-stack command to destroy the instance.

type ReadOnly

type ReadOnly struct {
	Fake *Fake
}

func NewReadOnly

func NewReadOnly(address string) *ReadOnly

NewReadOnly returns a mock of a cozy-stack client that can be used in tests, and it panics if any write operation is called.

func (*ReadOnly) Changes

func (ro *ReadOnly) Changes(seq *remote.Seq, limit int, skipTrashed bool) (*remote.ChangesResponse, error)

Changes is required by the remote.Client interface.

func (*ReadOnly) CreateDir

func (ro *ReadOnly) CreateDir(parentID remote.ID, name string) (*remote.Doc, error)

CreateDir is required by the remote.Client interface.

func (*ReadOnly) EmptyTrash

func (ro *ReadOnly) EmptyTrash() error

EmptyTrash is required by the remote.Client interface.

func (*ReadOnly) Refresh

func (ro *ReadOnly) Refresh() error

Refresh is required by the remote.Client interface.

func (*ReadOnly) Synchronized

func (ro *ReadOnly) Synchronized() error

Synchronized is required by the remote.Client interface.

func (*ReadOnly) Trash

func (ro *ReadOnly) Trash(doc *remote.Doc) (*remote.Doc, error)

Trash is required by the remote.Client interface.

type Stack

type Stack struct {
	Port  int
	FSDir string
	Cmd   *exec.Cmd
}

Stack can be used to control a cozy-stack running locally for tests.

func NewStack

func NewStack(port int, fsdir string) *Stack

NewStack builds a stack object, but you have to call Start on it to run the cozy-stack process.

func (*Stack) AdminPort

func (s *Stack) AdminPort() string

AdminPort returns the admin port as a string.

func (*Stack) CreateInstance

func (s *Stack) CreateInstance(name string) (*Instance, error)

CreateInstance creates an instance with a name like "alice".

func (*Stack) Start

func (s *Stack) Start() error

Start executes the cozy-stack serve command, and waits that the stack is listening to http requests.

func (*Stack) Stop

func (s *Stack) Stop() error

Stop stops the cozy-stack process.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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