Skip to content

Commit

Permalink
add mock app storage
Browse files Browse the repository at this point in the history
  • Loading branch information
erudenko committed Jul 10, 2023
1 parent 6026309 commit 66db835
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 0 deletions.
101 changes: 101 additions & 0 deletions storage/mock/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package mock

import (
"crypto/rand"
"errors"
"io"

"github.com/madappgang/identifo/v2/model"
"golang.org/x/exp/maps"
)

var _a model.AppStorage = &App{}

type App struct {
Apps map[string]model.AppData
}

func NewApp() *App {
a := App{
Apps: map[string]model.AppData{},
}
return &a
}

func (a *App) AppByID(id string) (model.AppData, error) {
for _, app := range a.Apps {
if app.ID == id {
return app, nil
}
}
return model.AppData{}, errors.New("not found")
}

func (a *App) ActiveAppByID(appID string) (model.AppData, error) {
app, err := a.AppByID(appID)
if err != nil {
return model.AppData{}, err
}
if !app.Active {
return model.AppData{}, errors.New("not found")
}
return app, nil
}

func (a *App) CreateApp(app model.AppData) (model.AppData, error) {
if len(app.ID) == 0 {
app.ID = randomID(10)
}
a.Apps[app.ID] = app
return app, nil
}

func (a *App) DisableApp(app model.AppData) error {
app, err := a.AppByID(app.ID)
if err != nil {
return err
}
app.Active = false
a.Apps[app.ID] = app
return nil
}

func (a *App) UpdateApp(appID string, newApp model.AppData) (model.AppData, error) {
a.Apps[appID] = newApp
return newApp, nil
}

func (a *App) FetchApps(filter string) ([]model.AppData, error) {
return maps.Values(a.Apps), nil
}

func (a *App) DeleteApp(id string) error {
delete(a.Apps, id)
return nil
}

func (a *App) ImportJSON(data []byte, cleanOldData bool) error {
// TODO: implement
return errors.New("not implemented")
}

func (a *App) TestDatabaseConnection() error {
return nil
}

func (a *App) Close() {
}

var table = [...]byte{'1', '2', '3', '4', '5', '6', '7', '8', '9', '0'}

func randomID(length int) string {
b := make([]byte, length)
n, err := io.ReadAtLeast(rand.Reader, b, length)
if n != length {
panic(err)
}
for i := 0; i < len(b); i++ {
b[i] = table[int(b[i])%len(table)]
}
return string(b)
}
29 changes: 29 additions & 0 deletions storage/user_controller_challenges_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package storage_test

import (
"context"
"testing"

"github.com/madappgang/identifo/v2/model"
"github.com/madappgang/identifo/v2/storage"
"github.com/stretchr/testify/require"
)

func TestRequestSMSTest(t *testing.T) {
cc := storage.UserStorageController{}

ch := model.UserAuthChallenge{
UserID: "u1",
DeviceID: "d1",
UserCodeChallenge: "ucc1234",
OTP: "123",
Strategy: model.FirstFactorInternalStrategy{
Challenge: model.AuthChallengeTypeOTP,
Transport: model.AuthTransportTypeSMS,
},
}
ch, err := cc.RequestChallenge(context.TODO(), ch)
require.NoError(t, err)
}


0 comments on commit 66db835

Please sign in to comment.