Golang eos-server-test.Assert类(方法)实例源码

下面列出了Golang eos-server-test.Assert 类(方法)源码代码实例,从而了解它的用法。

作者:GU-2013-TEAM-    项目:eo   
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: "b"}
	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)
}

作者:GU-2013-TEAM-    项目:eo   
//-------------------------------------------------------
// 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)
}

作者:GU-2013-TEAM-    项目:eo   
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)
}

作者:GU-2013-TEAM-    项目:eo   
//-------------------------------------------------------
// 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)
}

作者:GU-2013-TEAM-    项目:eo   
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()
}

作者:GU-2013-TEAM-    项目:eo   
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)
}

作者:GU-2013-TEAM-    项目:eo   
//-------------------------------------------------------
// 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)
}

作者:GU-2013-TEAM-    项目:eo   
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)

}

作者:GU-2013-TEAM-    项目:eo   
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()
}

作者:GU-2013-TEAM-    项目:eo   
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)

}

作者:GU-2013-TEAM-    项目:eo   
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)
}

作者:GU-2013-TEAM-    项目:eo   
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)
}

作者:GU-2013-TEAM-    项目:eo   
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)
}

作者:GU-2013-TEAM-    项目:eo   
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)
}

作者:GU-2013-TEAM-    项目:eo   
//-------------------------------------------------------
// 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)
}

作者:GU-2013-TEAM-    项目:eo   
//-------------------------------------------------------
// 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)
}

作者:GU-2013-TEAM-    项目:eo   
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")
}

作者:GU-2013-TEAM-    项目:eo   
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")
}

作者:GU-2013-TEAM-    项目:eo   
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)
}

作者:GU-2013-TEAM-    项目:eo   
//-------------------------------------------------------
// 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)
}


问题


面经


文章

微信
公众号

扫码关注公众号