auth

Paddy 2015-03-21 Parent:d30a3a12d387 Child:77db7c65216c

149:8267e1c8bcd1 Go to Latest

auth/oauth2_test.go

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.

History
     1.1 --- a/oauth2_test.go	Sat Mar 21 01:23:33 2015 -0400
     1.2 +++ b/oauth2_test.go	Sat Mar 21 14:53:15 2015 -0400
     1.3 @@ -50,7 +50,7 @@
     1.4  		ID:       uuid.NewID(),
     1.5  		ClientID: client.ID,
     1.6  		URI:      "https://test.secondbit.org/redirect",
     1.7 -		Added:    time.Now(),
     1.8 +		Added:    time.Now().Round(time.Millisecond),
     1.9  	}
    1.10  	err := testContext.SaveClient(client)
    1.11  	if err != nil {
    1.12 @@ -72,7 +72,7 @@
    1.13  		Active:    true,
    1.14  		ProfileID: profile.ID,
    1.15  		CSRFToken: "nosurf",
    1.16 -		Expires:   time.Now().Add(time.Hour),
    1.17 +		Expires:   time.Now().Round(time.Millisecond).Add(time.Hour),
    1.18  	}
    1.19  	err = testContext.CreateSession(session)
    1.20  	if err != nil {
    1.21 @@ -187,7 +187,7 @@
    1.22  		ID:        "testsession",
    1.23  		Active:    true,
    1.24  		CSRFToken: "nosurf",
    1.25 -		Expires:   time.Now().Add(time.Hour),
    1.26 +		Expires:   time.Now().Round(time.Millisecond).Add(time.Hour),
    1.27  	}
    1.28  	err = testContext.CreateSession(session)
    1.29  	if err != nil {
    1.30 @@ -262,7 +262,7 @@
    1.31  		ID:        "testsession",
    1.32  		Active:    true,
    1.33  		CSRFToken: "nosurf",
    1.34 -		Expires:   time.Now().Add(time.Hour),
    1.35 +		Expires:   time.Now().Round(time.Millisecond).Add(time.Hour),
    1.36  	}
    1.37  	err = testContext.CreateSession(session)
    1.38  	if err != nil {
    1.39 @@ -293,7 +293,7 @@
    1.40  		ID:       uuid.NewID(),
    1.41  		ClientID: client.ID,
    1.42  		URI:      "https://test.secondbit.org/redirect",
    1.43 -		Added:    time.Now(),
    1.44 +		Added:    time.Now().Round(time.Millisecond),
    1.45  	}
    1.46  	err = testContext.AddEndpoints(client.ID, []Endpoint{endpoint})
    1.47  	if err != nil {
    1.48 @@ -313,7 +313,7 @@
    1.49  		ID:       uuid.NewID(),
    1.50  		ClientID: client.ID,
    1.51  		URI:      "https://test.secondbit.org/redirect",
    1.52 -		Added:    time.Now(),
    1.53 +		Added:    time.Now().Round(time.Millisecond),
    1.54  	}
    1.55  	err = testContext.AddEndpoints(client.ID, []Endpoint{endpoint2})
    1.56  	if err != nil {
    1.57 @@ -366,7 +366,7 @@
    1.58  		ID:       uuid.NewID(),
    1.59  		ClientID: client.ID,
    1.60  		URI:      "https://test.secondbit.org/redirect",
    1.61 -		Added:    time.Now(),
    1.62 +		Added:    time.Now().Round(time.Millisecond),
    1.63  	}
    1.64  	err := testContext.SaveClient(client)
    1.65  	if err != nil {
    1.66 @@ -380,7 +380,7 @@
    1.67  		ID:        "testsession",
    1.68  		Active:    true,
    1.69  		CSRFToken: "nosurf",
    1.70 -		Expires:   time.Now().Add(time.Hour),
    1.71 +		Expires:   time.Now().Round(time.Millisecond).Add(time.Hour),
    1.72  	}
    1.73  	err = testContext.CreateSession(session)
    1.74  	if err != nil {
    1.75 @@ -472,7 +472,7 @@
    1.76  		ID:       uuid.NewID(),
    1.77  		ClientID: client.ID,
    1.78  		URI:      "https://test.secondbit.org/redirect",
    1.79 -		Added:    time.Now(),
    1.80 +		Added:    time.Now().Round(time.Millisecond),
    1.81  	}
    1.82  	err := testContext.SaveClient(client)
    1.83  	if err != nil {
    1.84 @@ -486,7 +486,7 @@
    1.85  		ID:        "testsession",
    1.86  		Active:    true,
    1.87  		CSRFToken: "nosurf",
    1.88 -		Expires:   time.Now().Add(time.Hour),
    1.89 +		Expires:   time.Now().Round(time.Millisecond).Add(time.Hour),
    1.90  	}
    1.91  	err = testContext.CreateSession(session)
    1.92  	if err != nil {
    1.93 @@ -600,7 +600,7 @@
    1.94  		ID:        "testsession",
    1.95  		Active:    true,
    1.96  		CSRFToken: "nosurf",
    1.97 -		Expires:   time.Now().Add(time.Hour),
    1.98 +		Expires:   time.Now().Round(time.Millisecond).Add(time.Hour),
    1.99  	}
   1.100  	err = testContext.CreateSession(session)
   1.101  	if err != nil {
   1.102 @@ -719,14 +719,14 @@
   1.103  		LockedUntil:            time.Time{},
   1.104  		PassphraseReset:        "",
   1.105  		PassphraseResetCreated: time.Time{},
   1.106 -		Created:                time.Now(),
   1.107 +		Created:                time.Now().Round(time.Millisecond),
   1.108  		LastSeen:               time.Time{},
   1.109  	}
   1.110  	login := Login{
   1.111  		Type:      "email",
   1.112  		Value:     "test@example.com",
   1.113  		ProfileID: profile.ID,
   1.114 -		Created:   time.Now(),
   1.115 +		Created:   time.Now().Round(time.Millisecond),
   1.116  		LastUsed:  time.Time{},
   1.117  	}
   1.118  	err := context.SaveProfile(profile)
   1.119 @@ -788,7 +788,7 @@
   1.120  	}
   1.121  	authCode := AuthorizationCode{
   1.122  		Code:        "testcode",
   1.123 -		Created:     time.Now(),
   1.124 +		Created:     time.Now().Round(time.Millisecond),
   1.125  		ExpiresIn:   600,
   1.126  		ClientID:    client.ID,
   1.127  		Scopes:      []string{"testscope"},