Test our Postgres profileStore implementation.
Update all our test cases to use time.Now().Round(time.Millisecond), because Go
uses nanosecond precision on time values, but Postgres silently truncates that
to millisecond precision. This caused our tests to report false failures that
were just silent precision loss, not actual failures.
Set up our authd server to use the Postgres store for profiles and automatically
create a test scope when starting up.
Log errors when creating Clients through the API, instead of just swallowing
them and sending back cryptic act of god errors.
Add a NewPostgres helper that returns a postgres profileStore from a connection
string (passed through pq transparently).
Add an Empty() bool helper to ProfileChange and BulkProfileChange types, so we
can determine if there are any changes we need to act on easily.
Log errors when creating Pofiles through the API, instead of just swalloing them
and sending back cryptic act of god errors.
Remove the ` quotes around field and table names, which are not supported in
Postgres. This required adding a few functions/methods to pan.
Detect situations where a profile was expected and not found, and return
ErrProfileNotFound.
Detect pq errors thrown when the profiles_pkey constraint is violated, and
transform them to the ErrProfileAlreadyExists error.
Detect empty ProfileChange and BulkProfileChange variables and abort the
updateProfile and updateProfiles methods early, before invalid SQL is generated.
Detect pq errors thrown when the logins_pkey constraint is violated, and
transform them to the ErrLoginAlreadyExists error.
Detect when removing a Login and no rows were affected, and return an
ErrLoginNotFound.
Create an sql dir with a postgres_init script that will initialize the schema of
the tables expected in the database.
8 "bitbucket.org/ww/goautoneg"
12 requestErrAccessDenied = "access_denied"
13 requestErrInsufficient = "insufficient"
14 requestErrOverflow = "overflow"
15 requestErrInvalidValue = "invalid_value"
16 requestErrInvalidFormat = "invalid_format"
17 requestErrMissing = "missing"
18 requestErrNotFound = "not_found"
19 requestErrConflict = "conflict"
20 requestErrActOfGod = "act_of_god"
24 actOfGodResponse = response{Errors: []requestError{requestError{Slug: requestErrActOfGod}}}
25 invalidFormatResponse = response{Errors: []requestError{requestError{Slug: requestErrInvalidFormat, Field: "/"}}}
27 encoders = []string{"application/json"}
30 type response struct {
31 Errors []requestError `json:"errors,omitempty"`
32 Logins []Login `json:"logins,omitempty"`
33 Profiles []Profile `json:"profiles,omitempty"`
34 Clients []Client `json:"clients,omitempty"`
35 Endpoints []Endpoint `json:"endpoints,omitempty"`
38 type requestError struct {
39 Slug string `json:"error,omitempty"`
40 Field string `json:"field,omitempty"`
41 Param string `json:"param,omitempty"`
42 Header string `json:"header,omitempty"`
45 func negotiate(h http.Handler) http.Handler {
46 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
47 if r.Header.Get("Accept") != "" {
48 contentType := goautoneg.Negotiate(r.Header.Get("Accept"), encoders)
49 if contentType == "" {
50 w.WriteHeader(http.StatusNotAcceptable)
51 w.Write([]byte("Unsupported content type requested: " + r.Header.Get("Accept")))
59 func encode(w http.ResponseWriter, r *http.Request, status int, resp response) {
60 contentType := goautoneg.Negotiate(r.Header.Get("Accept"), encoders)
61 w.Header().Set("content-type", contentType)
65 case "application/json":
66 enc := json.NewEncoder(w)
67 err = enc.Encode(resp)
69 enc := json.NewEncoder(w)
70 err = enc.Encode(resp)
77 func decode(r *http.Request, target interface{}) error {
79 switch r.Header.Get("Content-Type") {
80 case "application/json":
81 dec := json.NewDecoder(r.Body)
82 return dec.Decode(target)
84 dec := json.NewDecoder(r.Body)
85 return dec.Decode(target)
89 func wrap(context Context, f func(w http.ResponseWriter, r *http.Request, context Context)) http.Handler {
90 return negotiate(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {