Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: refactor migration tests to use fixtures #2936

Merged
merged 1 commit into from Jan 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion consent/manager.go
Expand Up @@ -33,7 +33,7 @@ type ForcedObfuscatedLoginSession struct {
SubjectObfuscated string `db:"subject_obfuscated"`
}

func (_ *ForcedObfuscatedLoginSession) TableName() string {
func (_ ForcedObfuscatedLoginSession) TableName() string {
return "hydra_oauth2_obfuscated_authentication_session"
}

Expand Down
2 changes: 2 additions & 0 deletions go.mod
Expand Up @@ -3,6 +3,7 @@ module github.com/ory/hydra
go 1.16

replace (
github.com/bradleyjkemp/cupaloy/v2 => github.com/aeneasr/cupaloy/v2 v2.6.1-0.20210924214125-3dfdd01210a3
github.com/dgrijalva/jwt-go => github.com/golang-jwt/jwt/v4 v4.0.0
github.com/gobuffalo/packr => github.com/gobuffalo/packr v1.30.1
github.com/gogo/protobuf => github.com/gogo/protobuf v1.3.2
Expand All @@ -14,6 +15,7 @@ replace (
)

require (
github.com/bradleyjkemp/cupaloy/v2 v2.6.0
github.com/bxcodec/faker/v3 v3.6.0
github.com/cenkalti/backoff/v3 v3.0.0
github.com/evanphx/json-patch v4.9.0+incompatible
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Expand Up @@ -107,6 +107,8 @@ github.com/Shopify/logrus-bugsnag v0.0.0-20171204204709-577dee27f20d/go.mod h1:H
github.com/Shopify/sarama v1.19.0/go.mod h1:FVkBWblsNy7DGZRfXLU0O9RCGt5g3g3yEuWXgklEdEo=
github.com/Shopify/toxiproxy v2.1.4+incompatible/go.mod h1:OXgGpZ6Cli1/URJOF1DMxUHB2q5Ap20/P/eIdh4G0pI=
github.com/VividCortex/gohistogram v1.0.0/go.mod h1:Pf5mBqqDxYaXu3hDrrU+w6nw50o/4+TcAqDqk/vUH7g=
github.com/aeneasr/cupaloy/v2 v2.6.1-0.20210924214125-3dfdd01210a3 h1:/SkiUr3JJzun9QN9cpUVCPri2ZwOFJ3ani+F3vdoCiY=
github.com/aeneasr/cupaloy/v2 v2.6.1-0.20210924214125-3dfdd01210a3/go.mod h1:bm7JXdkRd4BHJk9HpwqAI8BoAY1lps46Enkdqw6aRX0=
github.com/afex/hystrix-go v0.0.0-20180502004556-fa1af6a1f4f5/go.mod h1:SkGFH1ia65gfNATL8TAiHDNxPzPdmEL5uirI2Uyuz6c=
github.com/agnivade/levenshtein v1.0.1/go.mod h1:CURSv5d9Uaml+FovSIICkLbAUZ9S4RqaHDIsdSBg7lM=
github.com/ajg/form v0.0.0-20160822230020-523a5da1a92f h1:zvClvFQwU++UpIUBGC8YmDlfhUrweEy1R1Fj1gu5iIM=
Expand Down Expand Up @@ -175,8 +177,6 @@ github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 h1:DDGfHa7BWjL4Yn
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869/go.mod h1:Ekp36dRnpXw/yCqJaO+ZrUyxD+3VXMFFr56k5XYrpB4=
github.com/boombuler/barcode v1.0.0/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8=
github.com/boombuler/barcode v1.0.1/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8=
github.com/bradleyjkemp/cupaloy/v2 v2.6.0 h1:knToPYa2xtfg42U3I6punFEjaGFKWQRXJwj0JTv4mTs=
github.com/bradleyjkemp/cupaloy/v2 v2.6.0/go.mod h1:bm7JXdkRd4BHJk9HpwqAI8BoAY1lps46Enkdqw6aRX0=
github.com/bshuster-repo/logrus-logstash-hook v0.4.1/go.mod h1:zsTqEiSzDgAa/8GZR7E1qaXrhYNDKBYy5/dWPTIflbk=
github.com/buger/jsonparser v0.0.0-20180808090653-f4dd9f5a6b44/go.mod h1:bbYlZJ7hK1yFx9hf58LP0zeX7UjIGs20ufpu3evjr+s=
github.com/bugsnag/bugsnag-go v0.0.0-20141110184014-b1d153021fcd/go.mod h1:2oa8nejYd4cQ/b0hMIopN0lCRxU0bueqREvZLWFrtK8=
Expand Down
134 changes: 15 additions & 119 deletions persistence/sql/migratest/assertion_helpers.go
@@ -1,130 +1,26 @@
package migratest

import (
"database/sql"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/instana/testify/require"

"github.com/ory/hydra/client"
"github.com/ory/hydra/consent"
"github.com/ory/hydra/jwk"
"github.com/ory/hydra/oauth2"
sqlPersister "github.com/ory/hydra/persistence/sql"
"github.com/ory/hydra/flow"
"github.com/ory/x/sqlxx"
)

func assertEqualClients(t *testing.T, expected, actual *client.Client) {
now := time.Now()
expected.CreatedAt = now
expected.UpdatedAt = now
actual.CreatedAt = now
actual.UpdatedAt = now

assert.Equal(t, expected, actual)
}

func assertEqualJWKs(t *testing.T, expected, actual *jwk.SQLData) {
now := time.Now()
expected.CreatedAt = now
actual.CreatedAt = now

assert.Equal(t, expected, actual)
}

func assertEqualConsentRequests(t *testing.T, expected, actual *consent.ConsentRequest) {
now := time.Now()
expected.AuthenticatedAt = sqlxx.NullTime(now)
expected.RequestedAt = now
actual.AuthenticatedAt = sqlxx.NullTime(now)
actual.RequestedAt = now

assert.NotZero(t, actual.ClientID)
actual.ClientID = ""
assert.NotNil(t, actual.Client)
actual.Client = nil

assert.Equal(t, expected, actual)
}

func assertEqualLoginRequests(t *testing.T, expected, actual *consent.LoginRequest) {
now := time.Now()
expected.AuthenticatedAt = sqlxx.NullTime(now)
expected.RequestedAt = now
actual.AuthenticatedAt = sqlxx.NullTime(now)
actual.RequestedAt = now

assert.NotZero(t, actual.ClientID)
actual.ClientID = ""
assert.NotNil(t, actual.Client)
actual.Client = nil

assert.Equal(t, expected, actual)
}

func assertEqualLoginSessions(t *testing.T, expected, actual *consent.LoginSession) {
now := time.Now()
expected.AuthenticatedAt = sqlxx.NullTime(now)
actual.AuthenticatedAt = sqlxx.NullTime(now)

assert.Equal(t, expected, actual)
}

func assertEqualHandledConsentRequests(t *testing.T, expected, actual *consent.HandledConsentRequest) {
now := time.Now()
expected.AuthenticatedAt = sqlxx.NullTime(now)
expected.RequestedAt = now
actual.AuthenticatedAt = sqlxx.NullTime(now)
actual.RequestedAt = now
actual.HandledAt = sqlxx.NullTime{}

assert.Equal(t, expected, actual)
}

func assertEqualHandledLoginRequests(t *testing.T, expected, actual *consent.HandledLoginRequest) {
now := time.Now()
expected.AuthenticatedAt = sqlxx.NullTime(now)
expected.RequestedAt = now
actual.AuthenticatedAt = sqlxx.NullTime(now)
actual.RequestedAt = now

assert.Equal(t, expected, actual)
}

func assertEqualLogoutRequests(t *testing.T, expected, actual *consent.LogoutRequest) {
assert.NotZero(t, actual.ClientID)
actual.ClientID = sql.NullString{}

assert.Equal(t, expected, actual)
}

func assertEqualForcedObfucscatedLoginSessions(t *testing.T, expected, actual *consent.ForcedObfuscatedLoginSession) {
assert.NotNil(t, actual.ClientID)
actual.ClientID = ""
assert.Equal(t, expected, actual)
}

func assertEqualOauth2Data(t *testing.T, expected, actual *sqlPersister.OAuth2RequestSQL) {
now := time.Now()
expected.RequestedAt = now
actual.RequestedAt = now

assert.NotZero(t, actual.Client)
actual.Client = ""
if expected.ConsentChallenge.Valid {
assert.NotZero(t, actual.ConsentChallenge, "%+v", actual)
}
expected.ConsentChallenge = sql.NullString{}
actual.ConsentChallenge = sql.NullString{}

assert.Equal(t, expected, actual)
}

func assertEqualOauth2BlacklistedJTIs(t *testing.T, expected, actual *oauth2.BlacklistedJTI) {
now := time.Now()
expected.Expiry = now
actual.Expiry = now

assert.Equal(t, expected, actual)
func fixturizeFlow(t *testing.T, f *flow.Flow) {
require.NotZero(t, f.ClientID)
f.ClientID = ""
require.NotNil(t, f.Client)
f.Client = nil
recently := time.Now().Add(-time.Minute)
require.Greater(t, time.Time(f.LoginInitializedAt).UnixNano(), recently.UnixNano())
f.LoginInitializedAt = sqlxx.NullTime{}
require.True(t, f.RequestedAt.After(recently))
f.RequestedAt = time.Time{}
require.True(t, time.Time(f.LoginAuthenticatedAt).After(recently))
f.LoginAuthenticatedAt = sqlxx.NullTime{}
f.ConsentHandledAt = sqlxx.NullTime{}
}
32 changes: 32 additions & 0 deletions persistence/sql/migratest/fixtures/hydra_client/client-0001.json
@@ -0,0 +1,32 @@
{
"client_id": "client-0001",
"client_name": "Client 0001",
"client_secret": "secret-0001",
"redirect_uris": [
"http://redirect/0001_1"
],
"grant_types": [
"grant-0001_1"
],
"response_types": [
"response-0001_1"
],
"scope": "scope-0001",
"audience": [],
"owner": "owner-0001",
"policy_uri": "http://policy/0001",
"allowed_cors_origins": [],
"tos_uri": "http://tos/0001",
"client_uri": "http://client/0001",
"logo_uri": "http://logo/0001",
"contacts": [
"contact-0001_1"
],
"client_secret_expires_at": 0,
"subject_type": "",
"jwks": {},
"token_endpoint_auth_method": "none",
"created_at": "0001-01-01T00:00:00Z",
"updated_at": "0001-01-01T00:00:00Z",
"metadata": {}
}
32 changes: 32 additions & 0 deletions persistence/sql/migratest/fixtures/hydra_client/client-0002.json
@@ -0,0 +1,32 @@
{
"client_id": "client-0002",
"client_name": "Client 0002",
"client_secret": "secret-0002",
"redirect_uris": [
"http://redirect/0002_1"
],
"grant_types": [
"grant-0002_1"
],
"response_types": [
"response-0002_1"
],
"scope": "scope-0002",
"audience": [],
"owner": "owner-0002",
"policy_uri": "http://policy/0002",
"allowed_cors_origins": [],
"tos_uri": "http://tos/0002",
"client_uri": "http://client/0002",
"logo_uri": "http://logo/0002",
"contacts": [
"contact-0002_1"
],
"client_secret_expires_at": 0,
"subject_type": "",
"jwks": {},
"token_endpoint_auth_method": "none",
"created_at": "0001-01-01T00:00:00Z",
"updated_at": "0001-01-01T00:00:00Z",
"metadata": {}
}
34 changes: 34 additions & 0 deletions persistence/sql/migratest/fixtures/hydra_client/client-0003.json
@@ -0,0 +1,34 @@
{
"client_id": "client-0003",
"client_name": "Client 0003",
"client_secret": "secret-0003",
"redirect_uris": [
"http://redirect/0003_1"
],
"grant_types": [
"grant-0003_1"
],
"response_types": [
"response-0003_1"
],
"scope": "scope-0003",
"audience": [],
"owner": "owner-0003",
"policy_uri": "http://policy/0003",
"allowed_cors_origins": [],
"tos_uri": "http://tos/0003",
"client_uri": "http://client/0003",
"logo_uri": "http://logo/0003",
"contacts": [
"contact-0003_1"
],
"client_secret_expires_at": 0,
"subject_type": "",
"jwks": {},
"token_endpoint_auth_method": "none",
"request_object_signing_alg": "r_alg-0003",
"userinfo_signed_response_alg": "u_alg-0003",
"created_at": "0001-01-01T00:00:00Z",
"updated_at": "0001-01-01T00:00:00Z",
"metadata": {}
}
39 changes: 39 additions & 0 deletions persistence/sql/migratest/fixtures/hydra_client/client-0004.json
@@ -0,0 +1,39 @@
{
"client_id": "client-0004",
"client_name": "Client 0004",
"client_secret": "secret-0004",
"redirect_uris": [
"http://redirect/0004_1"
],
"grant_types": [
"grant-0004_1"
],
"response_types": [
"response-0004_1"
],
"scope": "scope-0004",
"audience": [],
"owner": "owner-0004",
"policy_uri": "http://policy/0004",
"allowed_cors_origins": [],
"tos_uri": "http://tos/0004",
"client_uri": "http://client/0004",
"logo_uri": "http://logo/0004",
"contacts": [
"contact-0004_1"
],
"client_secret_expires_at": 0,
"subject_type": "",
"sector_identifier_uri": "http://sector_id/0004",
"jwks_uri": "http://jwks/0004",
"jwks": {},
"token_endpoint_auth_method": "none",
"request_uris": [
"http://request/0004_1"
],
"request_object_signing_alg": "r_alg-0004",
"userinfo_signed_response_alg": "u_alg-0004",
"created_at": "0001-01-01T00:00:00Z",
"updated_at": "0001-01-01T00:00:00Z",
"metadata": {}
}
39 changes: 39 additions & 0 deletions persistence/sql/migratest/fixtures/hydra_client/client-0005.json
@@ -0,0 +1,39 @@
{
"client_id": "client-0005",
"client_name": "Client 0005",
"client_secret": "secret-0005",
"redirect_uris": [
"http://redirect/0005_1"
],
"grant_types": [
"grant-0005_1"
],
"response_types": [
"response-0005_1"
],
"scope": "scope-0005",
"audience": [],
"owner": "owner-0005",
"policy_uri": "http://policy/0005",
"allowed_cors_origins": [],
"tos_uri": "http://tos/0005",
"client_uri": "http://client/0005",
"logo_uri": "http://logo/0005",
"contacts": [
"contact-0005_1"
],
"client_secret_expires_at": 0,
"subject_type": "",
"sector_identifier_uri": "http://sector_id/0005",
"jwks_uri": "http://jwks/0005",
"jwks": {},
"token_endpoint_auth_method": "token_auth-0005",
"request_uris": [
"http://request/0005_1"
],
"request_object_signing_alg": "r_alg-0005",
"userinfo_signed_response_alg": "u_alg-0005",
"created_at": "0001-01-01T00:00:00Z",
"updated_at": "0001-01-01T00:00:00Z",
"metadata": {}
}