Commit 71c2ffef authored by Michel Roy's avatar Michel Roy
Browse files

add meep-users

parent 50449314
Loading
Loading
Loading
Loading
+10 −13
Original line number Diff line number Diff line
@@ -22,9 +22,6 @@ import (
	"strings"

	log "github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-logger"

	"github.com/lib/pq"
	_ "github.com/lib/pq"
)

// DB Config
@@ -91,8 +88,8 @@ func NewConnector(name, user, pwd, host, port string) (pc *Connector, err error)
	defer pc.db.Close()

	// Create DB if it does not exist
	// Use format: '<namespace>_<name>' & replace dashes with underscores
	pc.dbName = strings.ToLower(strings.Replace(namespace+"_"+name, "-", "_", -1))
	// Use format: '<name>' & replace dashes with underscores
	pc.dbName = strings.ToLower(strings.Replace(name, "-", "_", -1))

	// Ignore DB creation error in case it already exists.
	// Failure will occur at DB connection if DB was not successfully created.
@@ -277,7 +274,7 @@ func (pc *Connector) UpdateUser(username string, password string, role string, s
}

// GetUser - Get user information
func (pc *Connector) GetUser(name string) (user *User, err error) {
func (pc *Connector) GetUser(username string) (user *User, err error) {
	// Validate input
	if username == "" {
		err = errors.New("Missing username")
@@ -312,7 +309,7 @@ func (pc *Connector) GetUser(name string) (user *User, err error) {

	// Return error if not found
	if user == nil {
		err = errors.New("user not found: " + name)
		err = errors.New("user not found: " + username)
		return nil, err
	}
	return user, nil
@@ -355,7 +352,7 @@ func (pc *Connector) GetUsers() (userMap map[string]*User, err error) {
}

// DeleteUser - Delete user entry
func (pc *Connector) DeleteUser(name string) (err error) {
func (pc *Connector) DeleteUser(username string) (err error) {
	// Validate input
	if username == "" {
		err = errors.New("Missing username")
@@ -382,14 +379,14 @@ func (pc *Connector) DeleteUsers() (err error) {
}

//IsValidUser - does if user exists
func (pc *Connector) IsValidUser(username string) (bool, err error){
func (pc *Connector) IsValidUser(username string) (valid bool, err error){
	// Validate input
	if username == "" {
		err = errors.New("Missing username")
		return false, err
	}

	rows, err = pc.db.Query(`
	rows, err := pc.db.Query(`
		SELECT id
		FROM `+UsersTable+`
		WHERE name = ($1)`, username)
@@ -410,20 +407,20 @@ func (pc *Connector) IsValidUser(username string) (bool, err error){
			//User exists
			return true, nil
		}
	}
	// User does not exist & no error
	return false, nil
}
}

//AuthenticateUser - returns true or false if credentials are OK
func (pc *Connector) AuthenticateUser(username string, password string) (bool, err error){
func (pc *Connector) AuthenticateUser(username string, password string) (authenticated bool, err error){
	// Validate input
	if username == "" {
		err = errors.New("Missing username")
		return false, err
	}

	rows, err = pc.db.Query(`
	rows, err := pc.db.Query(`
		SELECT id
		FROM `+UsersTable+`
		WHERE name = ($1)
+29 −19
Original line number Diff line number Diff line
@@ -14,11 +14,10 @@
 * limitations under the License.
 */

package postgisdb
package usersdb

import (
	"fmt"
	"sort"
	"testing"

	log "github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-logger"
@@ -45,7 +44,7 @@ const (
	role0				= "invalid-role"
	role1				= "user"
	role2				= "user"
	role2				= "super"
	role3				= "super"

	sboxname0		= ""
	sboxname1		= "sbox-1"
@@ -79,7 +78,7 @@ func TestConnector(t *testing.T) {

	// Valid Connector
	fmt.Println("Create valid Postgis Connector")
	pc, err = NewConnector(pcName, pcNamespace, pcDBUser, pcDBPwd, pcDBHost, pcDBPort)
	pc, err = NewConnector(pcName, pcDBUser, pcDBPwd, pcDBHost, pcDBPort)
	if err != nil || pc == nil {
		t.Fatalf("Failed to create postgis Connector")
	}
@@ -167,13 +166,16 @@ func TestPostgisCreateUser(t *testing.T) {
	if user.password == password1 {
		t.Fatalf("Password not encrypted")
	}
	if !IsValidUser(username1){
	valid,err := pc.IsValidUser(username1)
	if err != nil || !valid {
		t.Fatalf("Failed to validate user")
	}
	if !AuthenticateUser(username1, password1) {
	valid,err = pc.AuthenticateUser(username1, password1)
	if err != nil || !valid {
		t.Fatalf("Failed to authenticate user")
	}
	if AuthenticateUser(username1, password2) {
	valid,err = pc.AuthenticateUser(username1, password2)
	if err == nil || valid {
		t.Fatalf("Wrong user authentication")
	}

@@ -181,7 +183,7 @@ func TestPostgisCreateUser(t *testing.T) {
	if err != nil {
		t.Fatalf("Failed to create asset")
	}
	user, err := pc.GetUser(username2)
	user, err = pc.GetUser(username2)
	if err != nil || user == nil {
		t.Fatalf("Failed to get user")
	}
@@ -191,13 +193,16 @@ func TestPostgisCreateUser(t *testing.T) {
	if user.password == password2 {
		t.Fatalf("Password not encrypted")
	}
	if !IsValidUser(username2){
	valid,err = pc.IsValidUser(username2)
	if err != nil || !valid {
		t.Fatalf("Failed to validate user")
	}
	if !AuthenticateUser(username2, password2) {
        valid,err = pc.AuthenticateUser(username2, password2)
        if err != nil || !valid {
		t.Fatalf("Failed to authenticate user")
	}
	if AuthenticateUser(username2, password1) {
        valid,err = pc.AuthenticateUser(username2, password1)
        if err == nil || valid {
		t.Fatalf("Wrong user authentication")
	}

@@ -205,7 +210,7 @@ func TestPostgisCreateUser(t *testing.T) {
	if err != nil {
		t.Fatalf("Failed to create asset")
	}
	user, err := pc.GetUser(username3)
	user, err = pc.GetUser(username3)
	if err != nil || user == nil {
		t.Fatalf("Failed to get user")
	}
@@ -215,19 +220,22 @@ func TestPostgisCreateUser(t *testing.T) {
	if user.password == password3 {
		t.Fatalf("Password not encrypted")
	}
	if !IsValidUser(username3){
	valid,err = pc.IsValidUser(username3)
	if err != nil || !valid {
		t.Fatalf("Failed to validate user")
	}
	if !AuthenticateUser(username3, password3) {
        valid,err = pc.AuthenticateUser(username3, password3)
        if err != nil || !valid {
		t.Fatalf("Failed to authenticate user")
	}
	if AuthenticateUser(username3, password1) {
        valid,err = pc.AuthenticateUser(username3, password2)
        if err == nil || valid {
		t.Fatalf("Wrong user authentication")
	}

	// Verify all additions worked
	userMap, err = pc.GetUsers()
	if err != nil || len(usersMap) != 3 {
	if err != nil || len(userMap) != 3 {
		t.Fatalf("Error getting all users")
	}

@@ -244,7 +252,7 @@ func TestPostgisCreateUser(t *testing.T) {

	// Update & validate update
	fmt.Println("Add user & validate update")
	err = UpdateUser(username1,password3,role3,sboxname3)
	err = pc.UpdateUser(username1,password3,role3,sboxname3)
	if err != nil {
		t.Fatalf("Failed to update asset")
	}
@@ -255,10 +263,12 @@ func TestPostgisCreateUser(t *testing.T) {
	if user.username != username1 || user.role != role3 || user.sboxname != sboxname3 {
		t.Fatalf("Wrong user data")
	}
	if !AuthenticateUser(username1, password3) {
	valid,err = pc.AuthenticateUser(username1,password3)
	if err != nil || !valid {
		t.Fatalf("Failed to authenticate user")
	}
	if AuthenticateUser(username1, password1) {
 	valid,err = pc.AuthenticateUser(username1,password1)
    	if err == nil || valid {
		t.Fatalf("Wrong user authentication")
	}