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) }
// removes all the stub data from the database func cleanTheDB() { fmt.Println("Cleaning up the DB, don't turn off...") // delete all the users db.DelTemps("users") // all the monitoring information for _, d := range daemons { db.C("monitoring_of_" + d.Id.Hex()).DropCollection() } // all the daemons db.DelTemps("daemons") // all the orgs db.DelTemps("organisations") fmt.Println("Bye!") }
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))) }
//------------------------------------------------------- // 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_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) }
//------------------------------------------------------- // 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) }
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) }
func Test_DaemonHandler(t *testing.T) { user := &User{ Id: "52a4ed348350a921bd000001", OrgId: NO_ORG, } lcmd := &CmdMessage{ Data: make(map[string]interface{}), Conn: &Connection{owner: user}, } lcmd.Data["daemon_id"] = "a" daemon := &Daemon{ Id: "a", OrgId: "Random_org", c: &Connection{}, Entry: &db.Daemon{}, } daemon.c.owner = daemon daemon.Authorise() // user has to be authorised, to get daemons data err := DaemonHandler(lcmd) test.Assert(err != nil, "user has to be authorised", t) // if the daemon does not exist, then an error is returned user.OrgId = "Anonymous" user.Authorise() err = DaemonHandler(lcmd) test.Assert(err != nil, "the daemon has to exist in the org", t) // if everything is ok, the daemon information is returned daemon.Deauthorise() daemon.OrgId = "Anonymous" daemon.Authorise() err = DaemonHandler(lcmd) test.Assert(err == nil, "it does not send the error", t) cmd := GetLastCmd() test.Assert(cmd.Data["daemon_id"].(string) == "a", "it returns the daemon information", t) //-------------------------------------------------- // if it is a daemon //-------------------------------------------------- // not sending the data msg := &Message{ msg: `{"type":"daemon","data":{"daemon_id":"id of the daemon"}}`, c: &Connection{owner: daemon}, } err, _ = HandleMsg(msg) test.Assert(err != nil, "it has to contain information", t) // when the data is sent, it stores it in the database msg.msg = `{ "type":"daemon", "data":{ "daemon_id":"id of the daemon", "daemon_platform":"Linux", "daemon_all_parameters":["cpu","ram","network"], "daemon_monitored_parameters":["cpu","ram"] } }` tmpD := &db.Daemon{OrgId: bson.ObjectIdHex("52a4ed348350a921bd000002"), Name: "a", Password: "******"} db.AddTemp("daemons", tmpD) daemon.Entry = tmpD daemon.Id = tmpD.Id.Hex() err, _ = HandleMsg(msg) test.Assert(err == nil, "no errors are raised", t) dbd := &db.Daemon{} db.C("daemons").FindId(tmpD.Id).One(dbd) test.Assert(dbd.Platform == "Linux", "it stores the platform in the database", t) test.Assert(len(dbd.Parameters) == 3, "it stores all the parameters", t) test.Assert(len(dbd.Monitored) == 2, "it also stores what it is monitoring", t) // it also sends new information to all the users in the org cmd = GetLastCmd() test.Assert(cmd.Type == "daemon", "it sends a daemon message", t) test.Assert(cmd.Data["daemon_id"].(string) == daemon.Id, "with the latest information about this daemon", t) // cleaning up daemon.Deauthorise() user.Deauthorise() db.DelTemps("daemons") }