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)
}
Exemplo n.º 2
0
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()
}
Exemplo n.º 3
0
//-------------------------------------------------------
// dummy users, to be removed
//-------------------------------------------------------
func createDummyUsers() {
	org := &db.Organisation{
		Id:   bson.ObjectIdHex("52f2bf8521db4a04d5000001"),
		Name: "Test organisation",
	}
	db.AddTemp("organisations", org)
	db.AddTemp("users", &db.User{
		OrgId:    org.Id,
		Email:    "*****@*****.**",
		Password: "******",
	})
	db.AddTemp("users", &db.User{
		OrgId:    org.Id,
		Email:    "*****@*****.**",
		Password: "******",
	})
}
Exemplo n.º 4
0
// create a user for that organisation
func generateUser(org *db.Organisation) {
	u := &db.User{
		OrgId:    org.Id,
		Email:    strconv.FormatInt(rand.Int63(), 10),
		Password: strconv.FormatInt(rand.Int63(), 10),
	}
	db.AddTemp("users", u)
	users = append(users, u)
}
Exemplo n.º 5
0
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)))
}
Exemplo n.º 6
0
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)
}
Exemplo n.º 7
0
// create a daemon for that organisation
func generateDaemon(org *db.Organisation) *db.Daemon {
	d := &db.Daemon{
		OrgId:      org.Id,
		Password:   strconv.FormatInt(rand.Int63(), 10),
		Name:       strconv.FormatInt(time.Now().UnixNano(), 10),
		Status:     "Running",
		Platform:   "Linux",
		Parameters: []string{"cpu"},
		Monitored:  []string{"cpu"},
	}
	db.AddTemp("daemons", d)
	// generating monitoring data for the daemon
	dataN := rand.Intn(100)
	for i := 0; i < dataN; i++ {
		dp := &db.Data{
			Parameter: "cpu",
			Time:      time.Now().Unix() - int64(rand.Intn(1000)),
			Value:     float64(rand.Intn(100)),
		}
		db.AddTemp("monitoring_of_"+d.Id.Hex(), dp)
	}
	daemons = append(daemons, d)
	return d
}
Exemplo n.º 8
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)
}
Exemplo n.º 9
0
// populates the database with stub data to match the counts
func setupDBStub(norg, nu, nd int) {
	fmt.Println("Setting up the database, might take a while")
	for i := 0; i < norg; i++ {
		org := &db.Organisation{Name: strconv.FormatInt(rand.Int63(), 10)}
		db.AddTemp("organisations", org)
		for j := 0; j < nu; j++ {
			generateUser(org)
		}
		ds := make([]*db.Daemon, 0, 100)
		for j := 0; j < nd; j++ {
			ds = append(ds, generateDaemon(org))
		}
		// saving for the future reference:
		o2d[org.Id] = ds
		fmt.Printf(".")
	}
	fmt.Println("\nDatabase is ready!\n")
}
Exemplo n.º 10
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)
}
Exemplo n.º 11
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)
}
Exemplo n.º 12
0
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)
}
Exemplo n.º 13
0
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")
}