func Test_LoginCheckHandler(t *testing.T) {
	tmp := &db.Session{UId: bson.ObjectIdHex("52a4ed348350a921bd000001")}
	db.AddTemp("sessions", tmp)
	tmpU := &db.User{Id: tmp.UId, OrgId: bson.ObjectIdHex("52a4ed348350a921bd000002"), Email: "a", Password: "******"}
	db.AddTemp("users", tmpU)

	msg := &Message{
		msg: `{"type":"loginCheck","data":{
            "session_id": "` + tmp.Id.Hex() + `"
        }}`,
		c: &Connection{owner: &User{}},
	}

	err, _ := HandleMsg(msg)

	cmd := GetLastCmd()
	test.Assert(cmd.Data["status"].(string) == "OK", "it recognises the previous session", t)

	db.DelTemps("sessions")
	db.DelTemps("users")

	HandleMsg(msg)

	cmd = GetLastCmd()
	test.Assert(cmd.Data["status"].(string) == "UNAUTHORIZED", "it does not authorise user when there is no previous session", t)

	msg.msg = `{"type":"loginCheck","data":{"session_id": "invalid"}}`
	err, _ = HandleMsg(msg)
	test.Assert(err != nil, "It returns an error if session id is invalid objectid", t)
	msg.msg = `{"type":"loginCheck","data":{"session_id": 5}}`
	err, _ = HandleMsg(msg)
	test.Assert(err != nil, "It returns an error if session id is invalid string", t)
}
Exemple #2
0
//-------------------------------------------------------
// getting the organisation
//-------------------------------------------------------
func Test_u_GetOrg(t *testing.T) {
	org := setupOrg()

	u := &User{OrgId: NO_ORG}
	test.Assert(u.GetOrg() == nil, "it does not return an org if the daemon is not authorised", t)

	u = &User{OrgId: "123"}
	test.Assert(u.GetOrg() == org, "it does return an org if the daemon is authorised", t)
}
Exemple #3
0
func Test_d_GetOrg(t *testing.T) {
	org := setupOrg()

	d := &Daemon{OrgId: NO_ORG}
	test.Assert(d.GetOrg() == nil, "it does not return an org if the daemon is not authorised", t)

	d = &Daemon{OrgId: "123"}
	test.Assert(d.GetOrg() == org, "it does return an org if the daemon is authorised", t)
}
//-------------------------------------------------------
// test handling the organisations
//-------------------------------------------------------
func Test_NewOrg(t *testing.T) {
	orgs = make(map[string]*Organisation)

	orgId := "123"
	org := NewOrg(orgId)

	test.Assert(org != nil, "it creates a new organisation", t)
	test.Assert(len(orgs) == 1, "it is stored in the map of orgs", t)
}
func Test_MonitoringHandlerUser(t *testing.T) {
	// before
	user := &User{OrgId: "Anonymous"}
	daemon := &Daemon{Id: "a", OrgId: "Anonymous"}
	user.Authorise()

	data1 := &db.Data{"", "cpu", 1000, 12.5}
	data2 := &db.Data{"", "cpu", 1500, 14.5}
	data3 := &db.Data{"", "cpu", 1600, 15.5}
	data4 := &db.Data{"", "cpu", 1900, 11.5}
	data5 := &db.Data{"", "ram", 1200, 9000}
	db.AddTemp("monitoring_of_a", data1)
	db.AddTemp("monitoring_of_a", data2)
	db.AddTemp("monitoring_of_a", data3)
	db.AddTemp("monitoring_of_a", data4)
	db.AddTemp("monitoring_of_a", data5)

	// let's try it from the string...
	msg := &Message{
		msg: `
        {
            "type": "monitoring",
            "data": {
                "daemon_id": "a",
                "parameter": "cpu",
                "from": 1100,
                "to": 1600
            }
        }
        `,
		c: &Connection{owner: user},
	}

	// the daemon is not in the org
	err, _ := HandleMsg(msg)

	test.Assert(err != nil, "it doesn't allow to monitor foreign daemons", t)

	// daemon exists in the org
	daemon.Authorise()

	err, _ = HandleMsg(msg)

	cmd := GetLastCmd()

	test.Assert(err == nil, "it does allow to monitor your daemons", t)
	vals := cmd.Data["values"].(map[string]float64)
	test.Assert(len(vals) == 2, "it returns the right number of answers", t)
	test.Assert(vals["1500"] == 14.5, "it has the correct data", t)
	test.Assert(vals["1600"] == 15.5, "it has the correct data", t)

	// cleaning up
	db.C("monitoring_of_a").DropCollection()
	user.Deauthorise()
	daemon.Deauthorise()
}
func Test_GetOrg(t *testing.T) {
	orgs = make(map[string]*Organisation)
	orgs["123"] = &Organisation{}

	org, err := GetOrg("123")
	test.Assert(err == nil && org != nil, "it gets an existing organisation", t)

	org, err = GetOrg("nonexistent")
	test.Assert(err != nil && org == nil, "it returns an error if org not found", t)
}
//-------------------------------------------------------
// test generating the json
//-------------------------------------------------------
func Test_GetMessage(t *testing.T) {
	cmd := &CmdMessage{Type: "test", Data: make(map[string]interface{})}
	cmd.Data["foo"] = "bar"
	cmd.Conn = &Connection{}
	msg, err := GetMessage(cmd)

	test.Assert(err == nil, "successfully stores the correct message", t)
	test.Assert(msg.msg == `{"type":"test","data":{"foo":"bar"}}`, "produces the expected output", t)
	test.Assert(msg.c == cmd.Conn, "preserves the connection", t)
	test.Assert(cmd.Conn != nil, "does not interfere with the structure", t)
}
func Test_addDaemon(t *testing.T) {
	org := setupOrg()

	test.Assert(len(org.Daemons) == 0, "there are no daemons initially", t)
	org.addDaemon("test", &Connection{})
	test.Assert(len(org.Daemons) == 1, "it adds a daemon to an organisation", t)

	err := org.addDaemon("test", &Connection{})
	test.Assert(err != nil, "it doesn't add a duplicate daemon", t)

}
func Test_DaemonsHandler(t *testing.T) {
	user := &User{
		Id:    "52a4ed348350a921bd000001",
		OrgId: NO_ORG,
	}
	msg := &Message{
		msg: `{"type":"daemons","data":{}}`,
		c:   &Connection{owner: user},
	}

	// user has to be authorised, to get daemons data
	err, _ := HandleMsg(msg)
	test.Assert(err != nil, "user has to be authorised", t)

	// when there are no daemons, it returns an empty list
	user.OrgId = "Anonymous"
	user.Authorise()

	err, _ = HandleMsg(msg)
	test.Assert(err == nil, "it does not throw an error for authorised user", t)

	cmd := GetLastCmd()
	test.Assert(len(cmd.Data["list"].([]map[string]interface{})) == 0, "it does not return inexistent daemons", t)

	// otherwise, it returns a list of daemons
	d1 := &Daemon{Id: "a", OrgId: "Anonymous", Entry: &db.Daemon{}}
	d2 := &Daemon{Id: "b", OrgId: "Anonymous", Entry: &db.Daemon{}}
	d3 := &Daemon{Id: "c", OrgId: "Another_org", Entry: &db.Daemon{}}
	// FIXME: these circular references look bad
	// we should probably refactor them away later on
	d1.c = &Connection{owner: d1}
	d2.c = &Connection{owner: d2}
	d3.c = &Connection{owner: d3}
	d1.Authorise()
	d2.Authorise()
	d3.Authorise()

	err, _ = HandleMsg(msg)

	cmd = GetLastCmd()
	test.Assert(len(cmd.Data["list"].([]map[string]interface{})) == 2, "it returns only daemons, that are in the same org, as user", t)

	// daemons cannot request it
	msg.c = &Connection{owner: &Daemon{}}

	err, _ = HandleMsg(msg)
	test.Assert(err != nil, "daemons are disallowed", t)

	// cleaning up
	user.Deauthorise()
	d1.Deauthorise()
	d2.Deauthorise()
	d3.Deauthorise()
}
func Test_addUser(t *testing.T) {
	org := setupOrg()

	test.Assert(len(org.Users) == 0, "there are no users initially", t)
	org.addUser("test", &Connection{})
	test.Assert(len(org.Users) == 1, "it adds a user to an organisation", t)

	err := org.addUser("test", &Connection{})
	test.Assert(err != nil, "it doesn't add a duplicate user", t)

}
Exemple #11
0
func Test_u_Authorise_exOrg(t *testing.T) {
	setupOrg()
	org := NewOrg("Anonymous")
	test.Assert(len(org.Users) == 0, "there are no daemons in the org initially", t)

	u := &User{Id: "test", OrgId: "Anonymous"}

	u.Authorise()

	test.Assert(len(org.Users) == 1, "It adds user to organisation", t)
	test.Assert(u.OrgId == "Anonymous", "It stores org ID in user", t)
}
Exemple #12
0
func Test_d_Authorise_exOrg(t *testing.T) {
	setupOrg()
	org := NewOrg("Anonymous")
	test.Assert(len(org.Daemons) == 0, "there are no daemons in the org initially", t)

	d := &Daemon{Id: "test", OrgId: "Anonymous"}

	d.Authorise()

	test.Assert(len(org.Daemons) == 1, "It adds daemon to organisation", t)
	test.Assert(d.OrgId == "Anonymous", "It stores org ID in daemon", t)
}
func Test_sendToDaemons(t *testing.T) {
	org := setupOrg()
	c1 := &Connection{send: make(chan string)}
	c2 := &Connection{send: make(chan string)}
	org.Daemons["test1"] = c1
	org.Daemons["test2"] = c2

	msg := "TestMsg"
	go org.sendToDaemons(msg)

	test.Assert(messageSent(msg, c1.send), "it sends the message to the first daemon's channel", t)
	test.Assert(messageSent(msg, c2.send), "it sends the message to the second daemon's channel", t)
}
Exemple #14
0
func Test_d_Authorise_newOrg(t *testing.T) {
	setupOrg()
	org, err := GetOrg("Anonymous")
	test.Assert(err != nil, "organisation does not exist", t)

	d := &Daemon{Id: "test", OrgId: "Anonymous"}

	d.Authorise()
	org, err = GetOrg("Anonymous")

	test.Assert(org != nil, "It creates a new org", t)
	test.Assert(len(org.Daemons) == 1, "It adds daemon to organisation", t)
	test.Assert(d.OrgId == "Anonymous", "It stores org ID in daemon", t)
}
Exemple #15
0
//-------------------------------------------------------
// remove itself from the organisation
//-------------------------------------------------------
func Test_u_Deauthorise(t *testing.T) {
	org := setupOrg()
	org.Users["test"] = &Connection{}

	u := &User{Id: "test", OrgId: NO_ORG}

	err := u.Deauthorise()
	test.Assert(err != nil, "it does not deauthorise already deauthorised user", t)

	u.OrgId = "123"
	err = u.Deauthorise()
	test.Assert(err == nil && len(org.Users) == 0, "it removes a user from organisation", t)
	test.Assert(u.OrgId == NO_ORG, "it sets the user organisation to none", t)
}
Exemple #16
0
//-------------------------------------------------------
// add yourself to a organisation
//-------------------------------------------------------
func Test_u_Authorise_newOrg(t *testing.T) {
	setupOrg()
	org, err := GetOrg("Anonymous")
	test.Assert(err != nil, "organisation does not exist", t)

	u := &User{Id: "test", OrgId: "Anonymous"}

	u.Authorise()
	org, err = GetOrg("Anonymous")

	test.Assert(org != nil, "It creates a new org", t)
	test.Assert(len(org.Users) == 1, "It adds user to organisation", t)
	test.Assert(u.OrgId == "Anonymous", "It stores org ID in user", t)
}
Exemple #17
0
func Test_RegisterHandler(t *testing.T) {
	spy, called := getHandlerSpy()

	oldLen := len(handlers)
	RegisterHandler("_test", spy)

	test.Assert(len(handlers)-oldLen == 1, "created a new handler", t)
	test.Assert(*called == false, "our spy works", t)
	handlers["_test"](&CmdMessage{})
	test.Assert(*called == true, "associated with specified function", t)

	// cleaning up
	delete(handlers, "_test")
}
Exemple #18
0
func Test_DeregisterHandler(t *testing.T) {
	spy, _ := getHandlerSpy()

	handlers["_test"] = spy

	oldLen := len(handlers)
	DeregisterHandler("_test")
	test.Assert(oldLen-len(handlers) == 1, "deletes the handler", t)
	err := DeregisterHandler("_again")
	test.Assert(err != nil, "fails when no such handler exists", t)

	// cleaning up
	delete(handlers, "_test")
}
Exemple #19
0
func Test_d_Deauthorise(t *testing.T) {
	org := setupOrg()
	org.Daemons["test"] = &Connection{}

	d := &Daemon{Id: "test", OrgId: NO_ORG}

	err := d.Deauthorise()
	test.Assert(err != nil, "it does not deauthorise already deauthorised daemon", t)

	d.OrgId = "123"
	err = d.Deauthorise()
	test.Assert(err == nil && len(org.Daemons) == 0, "it removes a user from organisation", t)
	test.Assert(d.OrgId == NO_ORG, "it sets the user organisation to none", t)
}
Exemple #20
0
//-------------------------------------------------------
// get the id from the session
//-------------------------------------------------------
func Test_u_AuthFromSession(t *testing.T) {
	tmp := &db.Session{UId: bson.ObjectIdHex("52a4ed348350a921bd000002")}
	db.AddTemp("sessions", tmp)

	uid, err := AuthFromSession(tmp.Id.Hex())

	test.Assert(uid.Hex() == "52a4ed348350a921bd000002", "It finds the user when he is in the session", t)
	test.Assert(err == nil, "It does not throw an error then", t)

	db.DelTemps("sessions")
	uid, err = AuthFromSession(tmp.Id.Hex())

	test.Assert(uid.Hex() != "52a4ed348350a921bd000002", "It does not find the user that is not in the session", t)
	test.Assert(err != nil, "It does throw an error then", t)
}
func Test_delDaemon(t *testing.T) {
	org := setupOrg()
	org.Daemons["test"] = &Connection{}

	org.delDaemon("test")
	test.Assert(len(org.Daemons) == 0, "it deletes the daemon", t)
}
func Test_delUser(t *testing.T) {
	org := setupOrg()
	org.Users["test"] = &Connection{}

	org.delUser("test")
	test.Assert(len(org.Users) == 0, "it deletes the user", t)
}
Exemple #23
0
//-------------------------------------------------------
// obtain all the data and proceed to authorisation
//-------------------------------------------------------
func Test_u_Authenticate(t *testing.T) {
	tmpU := &db.User{OrgId: bson.ObjectIdHex("52a4ed348350a921bd000002"), Email: "a", Password: "******"}
	db.AddTemp("users", tmpU)

	u := &User{}
	err := u.Authenticate(tmpU.Id)

	test.Assert(u.OrgId == "52a4ed348350a921bd000002", "It authenticates user if he exists in the database", t)
	test.Assert(err == nil, "And it does not throw an error in that case", t)

	db.DelTemps("users")

	u = &User{}
	err = u.Authenticate(tmpU.Id)

	test.Assert(u.OrgId != "52a4ed348350a921bd000002", "It fails to recognise  user if he does not exist in the database", t)
	test.Assert(err != nil, "It throws an error in that case", t)
}
Exemple #24
0
func Test_d_Authenticate(t *testing.T) {
	tmpD := &db.Daemon{OrgId: bson.ObjectIdHex("52a4ed348350a921bd000002"), Name: "a"}
	db.AddTemp("daemons", tmpD)

	d := &Daemon{}
	err := d.Authenticate(tmpD.Id)

	test.Assert(d.OrgId == "52a4ed348350a921bd000002", "It authenticates daemon if he exists in the database", t)
	test.Assert(err == nil, "And it does not throw an error in that case", t)

	db.DelTemps("daemons")

	d = &Daemon{}
	err = d.Authenticate(tmpD.Id)

	test.Assert(d.OrgId != "52a4ed348350a921bd000002", "It fails to recognise daemon if he does not exist in the database", t)
	test.Assert(err != nil, "It throws an error in that case", t)
}
func Test_DelOrg(t *testing.T) {
	orgs = make(map[string]*Organisation)
	orgs["123"] = &Organisation{
		Users:   make(map[string]*Connection),
		Daemons: make(map[string]*Connection),
	}

	orgs["123"].Users["test"] = &Connection{}
	err := DelOrg("123")

	test.Assert(err != nil, "it does not delete the empty organisation", t)

	delete(orgs["123"].Users, "test")
	err = DelOrg("123")

	org, err2 := GetOrg("123")
	test.Assert(err == nil && err2 != nil && org == nil, "it deletes the organisation", t)
}
func Test_sendToDaemon(t *testing.T) {
	org := setupOrg()
	c := &Connection{send: make(chan string)}
	org.Daemons["test"] = c

	msg := "TestMsg"
	go org.sendToDaemon("test", msg)

	test.Assert(messageSent(msg, c.send), "it sends the message to the users channel", t)
}
func Test_daemonLoginHandler(t *testing.T) {
	// when there is a daemon -----------------------------------------
	tmpD := &db.Daemon{
		Id:       bson.ObjectIdHex("52a4ed348350a921bd000001"),
		OrgId:    bson.ObjectIdHex("52a4ed348350a921bd000002"),
		Name:     "daemon",
		Password: "******",
	}
	db.AddTemp("daemons", tmpD)

	// a field missing
	lcmd := &CmdMessage{
		Data: make(map[string]interface{}),
		Conn: &Connection{owner: &Daemon{}},
	}
	lcmd.Data["org_id"] = "52a4ed348350a921bd000002"
	lcmd.Data["name"] = "daemon"

	err := LoginHandler(lcmd)

	test.Assert(err != nil, "it sends an error if there is a field missing", t)

	// wrong password
	lcmd.Data["password"] = "******"

	err = LoginHandler(lcmd)

	test.Assert(err != nil, "it sends an error if password is wrong", t)

	// all correct
	lcmd.Data["password"] = "******"

	err = LoginHandler(lcmd)

	cmd := GetLastCmd()
	test.Assert(cmd.Data["id"].(string) == "52a4ed348350a921bd000001", "it returns a daemon id", t)
	test.Assert(err == nil, "it does not throw an error", t)
	test.Assert(lcmd.Conn.owner.(*Daemon).Entry != nil, "it extracts the database information and puts it on the connection", t)

	db.DelTemps("daemons")

	// when there is no daemon ----------------------------------------
	err = LoginHandler(lcmd)

	test.Assert(err == nil, "it does not throw an error", t)
	cmd = GetLastCmd()
	err = db.C("daemons").FindId(bson.ObjectIdHex(cmd.Data["id"].(string))).One(tmpD)
	test.Assert(err == nil, "it creates a new daemon, if one does not exist", t)
	test.Assert(tmpD.Status == "NOT_KNOWN", "the daemon is set as unknown", t)

	db.C("daemons").RemoveId(bson.ObjectIdHex(cmd.Data["id"].(string)))
}
Exemple #28
0
func Test_RunCmd(t *testing.T) {
	spy, called := getHandlerSpy()
	goodCmd := &CmdMessage{Type: "_test"}
	badCmd := &CmdMessage{Type: "_error"}

	handlers["_test"] = spy
	handlers["_error"] = spy

	err := RunCmd(goodCmd)
	test.Assert(*called == true, "calls the function", t)
	test.Assert(err == nil, "does not throw without an error", t)

	*called = false
	err = RunCmd(badCmd)
	test.Assert(*called == true, "calls the another function", t)
	test.Assert(err != nil, "pipes through the error from it", t)

	// cleaning up
	delete(handlers, "_test")
	delete(handlers, "_error")
}
func Test_LogoutHandler(t *testing.T) {
	// a user is removed from the organisation
	// and his session is deleted
	tmpS := &db.Session{UId: bson.ObjectIdHex("52a4ed348350a921bd000001")}
	db.AddTemp("sessions", tmpS)

	user := &User{
		Id:        "52a4ed348350a921bd000001",
		SessionId: tmpS.GetId().Hex(),
		OrgId:     "Anonymous",
	}
	msg := &Message{
		msg: `{"type":"logout","data":{}}`,
		c:   &Connection{owner: user},
	}
	user.Authorise()

	test.Assert(user.IsAuthorised(), "user is authorised before logout", t)

	err, _ := HandleMsg(msg)

	test.Assert(err == nil, "it logs out successfully", t)
	test.Assert(!user.IsAuthorised(), "user is not in the organisation any more", t)
	err = db.C("sessions").Find(bson.M{
		"uid": bson.ObjectIdHex("52a4ed348350a921bd000001"),
	}).One(tmpS)
	test.Assert(err != nil, "there are no sessions for this user", t)

	// it does not break if session was already gone
	err, _ = HandleMsg(msg)
	test.Assert(err == nil, "it does not blow up without session", t)

	// a daemon is removed from the organisation
	daemon := &Daemon{
		Id:    "52a4ed348350a921bd000001",
		OrgId: "Anonymous",
	}
	msg = &Message{
		msg: `{"type":"logout","data":{}}`,
		c:   &Connection{owner: daemon},
	}
	daemon.Authorise()

	test.Assert(daemon.IsAuthorised(), "daemon is authorised before logout", t)

	err, _ = HandleMsg(msg)

	test.Assert(err == nil, "it logs out successfully", t)
	test.Assert(!daemon.IsAuthorised(), "daemon is not in the organisation any more", t)
}
func Test_userLoginHandler(t *testing.T) {
	tmpU := &db.User{Id: bson.ObjectIdHex("52a4ed348350a921bd000001"),
		OrgId: bson.ObjectIdHex("52a4ed348350a921bd000002"), Email: "*****@*****.**", Password: "******"}
	db.AddTemp("users", tmpU)

	// no password
	lcmd := &CmdMessage{Data: make(map[string]interface{}), Conn: &Connection{owner: &User{}}}
	lcmd.Data["email"] = "*****@*****.**"

	err := LoginHandler(lcmd)

	test.Assert(err != nil, "it sends an error if there is no password", t)

	// wrong password
	lcmd.Data["password"] = "******"

	err = LoginHandler(lcmd)

	test.Assert(err != nil, "it sends an error if password is wrong", t)

	// all correct
	lcmd.Data["password"] = "******"

	err = LoginHandler(lcmd)

	cmd := GetLastCmd()
	test.Assert(len(cmd.Data["session_id"].(string)) == 24, "it returns a session_id for this user", t)
	test.Assert(err == nil, "it does not throw an error", t)
	test.Assert(cmd.Data["id"].(string) == "52a4ed348350a921bd000001", "it returns a user id", t)
	session := &db.Session{}
	err = db.C("sessions").Find(bson.M{"_id": bson.ObjectIdHex(cmd.Data["session_id"].(string))}).One(session)
	test.Assert(err == nil, "it creates a session in the database", t)
	test.Assert(lcmd.Conn.owner.(*User).Entry != nil, "it extracts the database information and puts it on the connection", t)

	db.DelTemps("users")
	db.C("sessions").RemoveAll(bson.M{"uid": bson.ObjectIdHex("52a4ed348350a921bd000001")})

	// no user at all
	err = LoginHandler(lcmd)

	test.Assert(err != nil, "it sends an error if there is no such user", t)
}