Skip to content

Commit

Permalink
structured logging
Browse files Browse the repository at this point in the history
  • Loading branch information
hummerdmag committed Aug 18, 2024
1 parent cf26b21 commit ead1789
Show file tree
Hide file tree
Showing 137 changed files with 1,673 additions and 1,026 deletions.
5 changes: 2 additions & 3 deletions api_test/app_settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestAppSettings(t *testing.T) {
SetHeader("Digest", "SHA-256="+signature).
SetHeader("Content-Type", "application/json").
Expect(t).
// AssertFunc(dumpResponse).
// AssertFunc(dumpResponse(t)).
Type("json").
Status(200).
JSONSchema("../test/artifacts/api/app_settings_scheme.json").
Expand All @@ -45,15 +45,14 @@ func TestAppSettingsCORS(t *testing.T) {
SetHeader("Digest", "SHA-256="+signature).
SetHeader("Content-Type", "application/json").
Expect(t).
// AssertFunc(dumpResponse).
// AssertFunc(dumpResponse(t)).
AssertFunc(func(rsp *http.Response, r *http.Request) error {
header = rsp.Header
return nil
}).
Status(204).
Done()

fmt.Printf("Header %+v\n", header)
assert.Contains(t, header.Get("Access-Control-Allow-Origin"), "http://localhost:3000")
assert.Contains(t, header.Get("Access-Control-Allow-Methods"), "GET")
}
12 changes: 6 additions & 6 deletions api_test/invites_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestInvite(t *testing.T) {
SetHeader("Content-Type", "application/json").
BodyString(data).
Expect(t).
// AssertFunc(dumpResponse).
// AssertFunc(dumpResponse(t)).
Type("json").
Status(200).
AssertFunc(validateJSON(func(data map[string]interface{}) error {
Expand All @@ -47,7 +47,7 @@ func TestInvite(t *testing.T) {

require.NotEmpty(t, at)
data = fmt.Sprintf(`
{
{
"email": "%s",
"access_role": "%s",
"callback_url": "%s",
Expand All @@ -65,7 +65,7 @@ func TestInvite(t *testing.T) {
SetHeader("Content-Type", "application/json").
BodyString(data).
Expect(t).
AssertFunc(dumpResponse).
AssertFunc(dumpResponse(t)).
AssertFunc(validateJSON(func(data map[string]interface{}) error {
link = data["link"].(string)
return nil
Expand Down Expand Up @@ -108,7 +108,7 @@ func TestInviteWithCustomLoginAppURL(t *testing.T) {
SetHeader("Content-Type", "application/json").
BodyString(data).
Expect(t).
// AssertFunc(dumpResponse).
// AssertFunc(dumpResponse(t)).
Type("json").
Status(200).
AssertFunc(validateJSON(func(data map[string]interface{}) error {
Expand All @@ -120,7 +120,7 @@ func TestInviteWithCustomLoginAppURL(t *testing.T) {

require.NotEmpty(t, at)
data = fmt.Sprintf(`
{
{
"email": "%s",
"access_role": "%s",
"callback_url": "%s",
Expand All @@ -138,7 +138,7 @@ func TestInviteWithCustomLoginAppURL(t *testing.T) {
SetHeader("Content-Type", "application/json").
BodyString(data).
Expect(t).
AssertFunc(dumpResponse).
AssertFunc(dumpResponse(t)).
AssertFunc(validateJSON(func(data map[string]interface{}) error {
link = data["link"].(string)
return nil
Expand Down
10 changes: 5 additions & 5 deletions api_test/login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestLogin(t *testing.T) {
SetHeader("Content-Type", "application/json").
BodyString(data).
Expect(t).
// AssertFunc(dumpResponse).
// AssertFunc(dumpResponse(t)).
Type("json").
Status(200).
JSONSchema("../test/artifacts/api/jwt_token_with_refresh_scheme.json").
Expand All @@ -60,7 +60,7 @@ func TestLoginWithNoRefresh(t *testing.T) {
SetHeader("Content-Type", "application/json").
BodyString(data).
Expect(t).
// AssertFunc(dumpResponse).
// AssertFunc(dumpResponse(t)).
AssertFunc(validateJSON(func(data map[string]interface{}) error {
g.Expect(data).To(MatchKeys(IgnoreExtras|IgnoreMissing, Keys{
"access_token": Not(BeZero()),
Expand All @@ -81,7 +81,7 @@ func TestLoginWithWrongAppID(t *testing.T) {
request.Post("/auth/login").
SetHeader("X-Identifo-ClientID", "wrong_app_ID").
Expect(t).
AssertFunc(dumpResponse).
AssertFunc(dumpResponse(t)).
AssertFunc(validateJSON(func(data map[string]interface{}) error {
g.Expect(data["error"]).To(MatchAllKeys(Keys{
"id": Equal(string(l.ErrorStorageAPPFindByIDError)),
Expand Down Expand Up @@ -115,7 +115,7 @@ func TestLoginWithWrongSignature(t *testing.T) {
SetHeader("Content-Type", "application/json").
BodyString(data).
Expect(t).
AssertFunc(dumpResponse).
AssertFunc(dumpResponse(t)).
Status(400).
AssertFunc(validateJSON(func(data map[string]interface{}) error {
g.Expect(data["error"]).To(MatchAllKeys(Keys{
Expand Down Expand Up @@ -145,7 +145,7 @@ func TestLoginTokenClaims(t *testing.T) {
SetHeader("Content-Type", "application/json").
BodyString(data).
Expect(t).
// AssertFunc(dumpResponse).
// AssertFunc(dumpResponse(t)).
Type("json").
AssertFunc(validateJSON(func(data map[string]interface{}) error {
tokenStr = data["access_token"].(string)
Expand Down
13 changes: 8 additions & 5 deletions api_test/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/caarlos0/env"
"github.com/joho/godotenv"
"github.com/madappgang/identifo/v2/config"
"github.com/madappgang/identifo/v2/logging"
"github.com/madappgang/identifo/v2/model"
"github.com/madappgang/identifo/v2/services/mail/mock"
"gopkg.in/h2non/baloo.v3"
Expand Down Expand Up @@ -91,7 +92,7 @@ func runServer() (model.Server, *http.Server) {
settings, _ = model.ConfigStorageSettingsFromString("file://../test/artifacts/api/config-mongo.yaml")
}

configStorage, err := config.InitConfigurationStorage(settings)
configStorage, err := config.InitConfigurationStorage(logging.DefaultLogger, settings)
if err != nil {
log.Fatalf("Unable to load config with error: %v", err)
}
Expand Down Expand Up @@ -143,10 +144,12 @@ func stopServer(server *http.Server) {
// Helper functions

// DumpResponse
func dumpResponse(res *http.Response, req *http.Request) error {
data, err := httputil.DumpResponse(res, true)
fmt.Printf("Response: %s \n", string(data))
return err
func dumpResponse(t *testing.T) func(res *http.Response, req *http.Request) error {
return func(res *http.Response, req *http.Request) error {
data, err := httputil.DumpResponse(res, true)
t.Logf("Response: %s \n", string(data))
return err
}
}

// DumpResponse
Expand Down
12 changes: 6 additions & 6 deletions api_test/management_invite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestManagementResetToken(t *testing.T) {
Expires: time.Now().Add(time.Hour).Unix(),
Host: u.Host,
}
fmt.Println(sd.String())
t.Log(sd.String())
signature := sig.SignString(sd.String(), []byte(cfg.ManagementKeySecret1))

request.Post("/management/token/reset_password").
Expand All @@ -42,7 +42,7 @@ func TestManagementResetToken(t *testing.T) {
SetHeader(sig.KeyIDHeaderKey, cfg.ManagementKeyID1).
BodyString(data).
Expect(t).
AssertFunc(dumpResponse).
AssertFunc(dumpResponse(t)).
Type("json").
Status(200).
AssertFunc(validateJSON(func(data map[string]interface{}) error {
Expand Down Expand Up @@ -77,7 +77,7 @@ func TestManagementInactiveKey(t *testing.T) {
Expires: time.Now().Add(time.Hour).Unix(),
Host: u.Host,
}
fmt.Println(sd.String())
t.Log(sd.String())
signature := sig.SignString(sd.String(), []byte(cfg.ManagementKeySecret2))

body := ""
Expand All @@ -91,7 +91,7 @@ func TestManagementInactiveKey(t *testing.T) {
SetHeader(sig.KeyIDHeaderKey, cfg.ManagementKeyID2).
BodyString(data).
Expect(t).
AssertFunc(dumpResponse).
AssertFunc(dumpResponse(t)).
Status(400).
AssertFunc(validateBodyText(func(b string) error {
body = b
Expand All @@ -115,7 +115,7 @@ func TestManagementInviteNoKeyID(t *testing.T) {
SetHeader("Content-Type", "application/json").
BodyString(data).
Expect(t).
AssertFunc(dumpResponse).
AssertFunc(dumpResponse(t)).
Status(400).
AssertFunc(validateBodyText(func(b string) error {
body = b
Expand All @@ -141,7 +141,7 @@ func TestManagementInviteWrongKeyID(t *testing.T) {
SetHeader("X-Nl-Key-Id", "AABBCCDDSS").
BodyString(data).
Expect(t).
AssertFunc(dumpResponse).
AssertFunc(dumpResponse(t)).
Status(400).
AssertFunc(validateBodyText(func(b string) error {
body = b
Expand Down
4 changes: 2 additions & 2 deletions api_test/misconfig_test/cors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ func TestCorsAlwaysTrue(t *testing.T) {
message = err["message"].(string)
return nil
})).
AssertFunc(dumpResponse).
AssertFunc(dumpResponse(t)).
Status(500).
Done()
assert.NotEmpty(t, message)
assert.Contains(t, message, "DefaultStorage settings: unsupported database type wrong_type")
assert.Contains(t, message, "DefaultStorage settings: unsupported database type 'wrong_type'")
}
13 changes: 8 additions & 5 deletions api_test/misconfig_test/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/caarlos0/env"
"github.com/joho/godotenv"
"github.com/madappgang/identifo/v2/config"
"github.com/madappgang/identifo/v2/logging"
"github.com/madappgang/identifo/v2/model"
"gopkg.in/h2non/baloo.v3"
"gopkg.in/h2non/baloo.v3/assert"
Expand Down Expand Up @@ -69,7 +70,7 @@ func runServer() (model.Server, *httptest.Server) {
os.Remove("./db.db")
settings, _ = model.ConfigStorageSettingsFromString("file://./config.yaml")

configStorage, err := config.InitConfigurationStorage(settings)
configStorage, err := config.InitConfigurationStorage(logging.DefaultLogger, settings)
if err != nil {
log.Fatalf("Unable to load config with error: %v", err)
}
Expand All @@ -95,10 +96,12 @@ func stopServer(server *httptest.Server) {
// Helper functions

// DumpResponse
func dumpResponse(res *http.Response, req *http.Request) error {
data, err := httputil.DumpResponse(res, true)
fmt.Printf("Response: %s \n", string(data))
return err
func dumpResponse(t *testing.T) func(res *http.Response, req *http.Request) error {
return func(res *http.Response, req *http.Request) error {
data, err := httputil.DumpResponse(res, true)
t.Logf("Response: %s \n", string(data))
return err
}
}

// DumpResponse
Expand Down
11 changes: 7 additions & 4 deletions api_test/mongo_app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"os"
"testing"

"github.com/madappgang/identifo/v2/logging"
"github.com/madappgang/identifo/v2/model"
"github.com/madappgang/identifo/v2/storage/mongo"
"github.com/stretchr/testify/assert"
Expand All @@ -17,10 +18,12 @@ func TestAppCRUD(t *testing.T) {

connStr := os.Getenv("IDENTIFO_STORAGE_MONGO_CONN")

s, err := mongo.NewAppStorage(model.MongoDatabaseSettings{
ConnectionString: connStr,
DatabaseName: "test_users",
})
s, err := mongo.NewAppStorage(
logging.DefaultLogger,
model.MongoDatabaseSettings{
ConnectionString: connStr,
DatabaseName: "test_users",
})
require.NoError(t, err)

expectedApp := model.AppData{
Expand Down
11 changes: 7 additions & 4 deletions api_test/mongo_user_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"os"
"testing"

"github.com/madappgang/identifo/v2/logging"
"github.com/madappgang/identifo/v2/model"
"github.com/madappgang/identifo/v2/storage/mongo"
"github.com/stretchr/testify/assert"
Expand All @@ -18,10 +19,12 @@ func TestFetchUser(t *testing.T) {

connStr := os.Getenv("IDENTIFO_STORAGE_MONGO_CONN")

s, err := mongo.NewUserStorage(model.MongoDatabaseSettings{
ConnectionString: connStr,
DatabaseName: "test_users",
})
s, err := mongo.NewUserStorage(
logging.DefaultLogger,
model.MongoDatabaseSettings{
ConnectionString: connStr,
DatabaseName: "test_users",
})
require.NoError(t, err)

s.(*mongo.UserStorage).ClearAllUserData()
Expand Down
Loading

0 comments on commit ead1789

Please sign in to comment.