Skip to content

Commit

Permalink
[upd][#44] refactored auth and db mocks. Everything compiles but 3 st…
Browse files Browse the repository at this point in the history
…orageservice tests are failing. (TestStorageResourceFlow, TestStorageUploadResourceDuplicated, TestStorageDeleteJobDuplicated)
  • Loading branch information
Federico Maggi committed Sep 6, 2016
1 parent 5d33320 commit cd06e93
Show file tree
Hide file tree
Showing 10 changed files with 203 additions and 409 deletions.
7 changes: 4 additions & 3 deletions authserver/serve_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
import (
auth "github.com/nexocrew/3nigm4/lib/auth/server"
db "github.com/nexocrew/3nigm4/lib/database/client"
dty "github.com/nexocrew/3nigm4/lib/database/types"
)

// Third party libs
Expand Down Expand Up @@ -49,13 +50,13 @@ func init() {
// in unit-tests, do not mess with it for other reasons.
// The default, production targeting, implementation uses Mongodb
// as backend database system.
var databaseStartup func(*args) (db.Database, error) = mgoStartup
var databaseStartup func(*args) (dty.Database, error) = mgoStartup

// mgoStartup implement startup logic for a mongodb based database
// connection.
func mgoStartup(arguments *args) (db.Database, error) {
func mgoStartup(arguments *args) (dty.Database, error) {
// startup db
mgodb, err := db.MgoSession(&db.DbArgs{
mgodb, err := db.MgoSession(&dty.DbArgs{
Addresses: strings.Split(arguments.dbAddresses, ","),
User: arguments.dbUsername,
Password: arguments.dbPassword,
Expand Down
64 changes: 32 additions & 32 deletions authserver/serve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ import (

// Internal dependencies.
import (
ty "github.com/nexocrew/3nigm4/lib/auth/types"
db "github.com/nexocrew/3nigm4/lib/database/client"
aty "github.com/nexocrew/3nigm4/lib/auth/types"
"github.com/nexocrew/3nigm4/lib/database/mock"
dty "github.com/nexocrew/3nigm4/lib/database/types"
"github.com/nexocrew/3nigm4/lib/itm"
"github.com/nexocrew/3nigm4/lib/logger"
wq "github.com/nexocrew/3nigm4/lib/workingqueue"
Expand All @@ -34,8 +34,8 @@ import (
// Default bcrypt iterations
const kBCryptIterations = 10

func mockStartup(arguments *args) (db.Database, error) {
mockdb := dbmock.NewMockDb(&db.DbArgs{
func mockStartup(arguments *args) (dty.Database, error) {
mockdb := dbmock.NewMockDb(&dty.DbArgs{
Addresses: strings.Split(arguments.dbAddresses, ","),
User: arguments.dbUsername,
Password: arguments.dbPassword,
Expand All @@ -59,16 +59,16 @@ func prepareMockDb(db *dbmock.Mockdb) error {
if err != nil {
return err
}
err = db.SetUser(&ty.User{
err = db.SetUser(&aty.User{
Username: "userA",
FullName: "user A",
Email: "[email protected]",
IsDisabled: false,
HashedPassword: hash,
Permissions: ty.Permissions{
Permissions: aty.Permissions{
SuperAdmin: false,
Services: map[string]ty.Level{
"test": ty.LevelAdmin,
Services: map[string]aty.Level{
"test": aty.LevelAdmin,
},
},
})
Expand All @@ -81,13 +81,13 @@ func prepareMockDb(db *dbmock.Mockdb) error {
if err != nil {
return err
}
err = db.SetUser(&ty.User{
err = db.SetUser(&aty.User{
Username: "asuperadmin",
FullName: "Super Admin",
Email: "[email protected]",
IsDisabled: false,
HashedPassword: hash,
Permissions: ty.Permissions{
Permissions: aty.Permissions{
SuperAdmin: true,
},
})
Expand Down Expand Up @@ -185,8 +185,8 @@ func TestRPCServe(t *testing.T) {
t.Logf("Client connected to %s.\n", address)

// login
var loginResponse ty.LoginResponseArg
err = client.Call("Login.Login", &ty.LoginRequestArg{
var loginResponse aty.LoginResponseArg
err = client.Call("Login.Login", &aty.LoginRequestArg{
Username: "userA",
Password: "passwordA",
}, &loginResponse)
Expand All @@ -200,8 +200,8 @@ func TestRPCServe(t *testing.T) {
t.Fatalf("Invalid token size, having %d expecting 64.\n", len(loginResponse.Token))
}
// session validation
var sessionResponse ty.AuthenticateResponseArg
err = client.Call("SessionAuth.Authenticate", &ty.AuthenticateRequestArg{
var sessionResponse aty.AuthenticateResponseArg
err = client.Call("SessionAuth.Authenticate", &aty.AuthenticateRequestArg{
Token: loginResponse.Token,
}, &sessionResponse)
if err != nil {
Expand All @@ -211,19 +211,19 @@ func TestRPCServe(t *testing.T) {
t.Fatalf("Wrong username, having %s expecting \"userA\".\n", sessionResponse.Username)
}
// userinfo
var userinfoResponse ty.UserInfoResponseArg
err = client.Call("SessionAuth.UserInfo", &ty.AuthenticateRequestArg{
var userinfoResponse aty.UserInfoResponseArg
err = client.Call("SessionAuth.UserInfo", &aty.AuthenticateRequestArg{
Token: loginResponse.Token,
}, &userinfoResponse)
if err != nil {
t.Fatalf("Unable to get userinfo: %s.\n", err.Error())
}
if userinfoResponse.Permissions.Services["test"] != ty.LevelAdmin {
t.Fatalf("Wrong auth level, having %d expecting %d.\n", userinfoResponse.Permissions.Services["test"], ty.LevelAdmin)
if userinfoResponse.Permissions.Services["test"] != aty.LevelAdmin {
t.Fatalf("Wrong auth level, having %d expecting %d.\n", userinfoResponse.Permissions.Services["test"], aty.LevelAdmin)
}
// logout
var logoutResponse ty.LogoutResponseArg
err = client.Call("Login.Logout", &ty.LogoutRequestArg{
var logoutResponse aty.LogoutResponseArg
err = client.Call("Login.Logout", &aty.LogoutRequestArg{
Token: loginResponse.Token,
}, &logoutResponse)
if err != nil {
Expand All @@ -245,8 +245,8 @@ func TestRPCServeSuperAdmin(t *testing.T) {
t.Logf("Client connected to %s.\n", address)

// login
var loginResponse ty.LoginResponseArg
err = client.Call("Login.Login", &ty.LoginRequestArg{
var loginResponse aty.LoginResponseArg
err = client.Call("Login.Login", &aty.LoginRequestArg{
Username: "asuperadmin",
Password: "passwordS",
}, &loginResponse)
Expand All @@ -260,23 +260,23 @@ func TestRPCServeSuperAdmin(t *testing.T) {
t.Fatalf("Invalid token size, having %d expecting 64.\n", len(loginResponse.Token))
}
// create user
var voidResponse ty.VoidResponseArg
var voidResponse aty.VoidResponseArg
hash, err := bcryptPassword("passwordB")
if err != nil {
t.Fatalf("Unable to bcrypt password: %s.\n", err.Error())
}
err = client.Call("SessionAuth.UpsertUser", &ty.UpserUserRequestArg{
err = client.Call("SessionAuth.UpsertUser", &aty.UpserUserRequestArg{
Token: loginResponse.Token,
User: ty.User{
User: aty.User{
Username: "userB",
FullName: "user B",
Email: "[email protected]",
IsDisabled: false,
HashedPassword: hash,
Permissions: ty.Permissions{
Permissions: aty.Permissions{
SuperAdmin: false,
Services: map[string]ty.Level{
"test": ty.LevelAdmin,
Services: map[string]aty.Level{
"test": aty.LevelAdmin,
},
},
},
Expand All @@ -285,24 +285,24 @@ func TestRPCServeSuperAdmin(t *testing.T) {
t.Fatalf("Unable to upsert user: %S.\n", err.Error())
}
// remove user
err = client.Call("SessionAuth.RemoveUser", &ty.RemoveUserRequestArg{
err = client.Call("SessionAuth.RemoveUser", &aty.RemoveUserRequestArg{
Token: loginResponse.Token,
Username: "userB",
}, &voidResponse)
if err != nil {
t.Fatalf("Unable to remove user: %s.\n", err.Error())
}
// kick off all sessions
err = client.Call("SessionAuth.KickOutAllSessions", &ty.AuthenticateRequestArg{
err = client.Call("SessionAuth.KickOutAllSessions", &aty.AuthenticateRequestArg{
Token: loginResponse.Token,
}, &voidResponse)
if err != nil {
t.Fatalf("Unable to remove all sessions: %s.\n", err.Error())
}

// logout
var logoutResponse ty.LogoutResponseArg
err = client.Call("Login.Logout", &ty.LogoutRequestArg{
var logoutResponse aty.LogoutResponseArg
err = client.Call("Login.Logout", &aty.LogoutRequestArg{
Token: loginResponse.Token,
}, &logoutResponse)
if err == nil {
Expand Down
6 changes: 3 additions & 3 deletions lib/auth/server/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@ import (
)

import (
db "github.com/nexocrew/3nigm4/lib/database/client"
dty "github.com/nexocrew/3nigm4/lib/database/types"
)

// Global vars protecting mutex.
var mtx sync.Mutex

// Runtime allocated global base database instance.
var dbclient db.Database
var dbclient dty.Database

// SetGlobalDbClient must be called to set the global db client,
// that implements the Database interface, to be used by RPC
// exposed functions. This function must be always invoked before
// proceeding registering other fucntions.
func SetGlobalDbClient(database db.Database) {
func SetGlobalDbClient(database dty.Database) {
mtx.Lock()
dbclient = database
mtx.Unlock()
Expand Down
43 changes: 3 additions & 40 deletions lib/database/client/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,43 +41,6 @@ const (
kMaxSessionExistance = 24 * time.Hour
)

// DbArgs is the exposed arguments
// required by each database interface
// implementing structs.
type DbArgs struct {
Addresses []string // cluster addresses in form <addr>:<port>;
User string // authentication username;
Password string // authentication password;
AuthDb string // the auth db.
}

// Database an interface defining a generic
// db, package targeting, implementation.
type Database interface {
// db client related functions
Copy() Database // retain the db client in a multi-coroutine environment;
Close() // release the client;
// user behaviour
GetUser(string) (*aty.User, error) // gets a user struct from an argument username;
SetUser(*aty.User) error // creates a new user in the db;
RemoveUser(string) error // remove an user from the db;
// session behaviour
GetSession([]byte) (*aty.Session, error) // search for a session in the db;
SetSession(*aty.Session) error // insert a session in the db;
RemoveSession([]byte) error // remove an existing session;
RemoveAllSessions() error // remove all sessions in the db.
// db create file log
SetFileLog(fl *dty.FileLog) error // add a new file log when a file is uploaded;
UpdateFileLog(fl *dty.FileLog) error // update an existing file log;
GetFileLog(file string) (*dty.FileLog, error) // get infos to a previously uploaded file;
RemoveFileLog(file string) error // remove a previously added file log;
// async tx
SetAsyncTx(at *dty.AsyncTx) error // add a new async tx record;
UpdateAsyncTx(at *dty.AsyncTx) error // update an existing async tx;
GetAsyncTx(id string) (*dty.AsyncTx, error) // get an existing tx;
RemoveAsyncTx(id string) error // remove an existing tx (typically should be done automatically with a ttl setup).
}

// Mongodb database, wrapping mgo session
// structure.
type Mongodb struct {
Expand All @@ -92,7 +55,7 @@ type Mongodb struct {
}

// composeDbAddress compose a string starting from dbArgs slice.
func composeDbAddress(args *DbArgs) string {
func composeDbAddress(args *dty.DbArgs) string {
dbAccess := fmt.Sprintf("mongodb://%s:%s@", args.User, args.Password)
for idx, addr := range args.Addresses {
dbAccess += addr
Expand All @@ -106,7 +69,7 @@ func composeDbAddress(args *DbArgs) string {

// MgoSession get a new session starting from the standard args
// structure.
func MgoSession(args *DbArgs) (*Mongodb, error) {
func MgoSession(args *dty.DbArgs) (*Mongodb, error) {
s, err := mgo.Dial(composeDbAddress(args))
if err != nil {
return nil, err
Expand Down Expand Up @@ -138,7 +101,7 @@ func MgoSession(args *DbArgs) (*Mongodb, error) {
}

// Copy the internal session to permitt multi corutine usage.
func (d *Mongodb) Copy() Database {
func (d *Mongodb) Copy() dty.Database {
return &Mongodb{
session: d.session.Copy(),
database: d.database,
Expand Down
Loading

0 comments on commit cd06e93

Please sign in to comment.