Пример #1
0
func TestFetchAdminAccounts(t *testing.T) {
	db := modeltesthelper.NewMongoDB(t)
	defer db.Close()

	acc1 := createTestAccount(t)
	defer modelhelper.RemoveAccount(acc1.Id)

	acc2 := createTestAccount(t)
	defer modelhelper.RemoveAccount(acc2.Id)

	group, err := createGroup()
	if err != nil {
		t.Error(err)
	}

	accounts, err := modelhelper.FetchAdminAccounts(group.Slug)
	if err != nil {
		t.Error(err)
	}

	if len(accounts) != 0 {
		t.Errorf("accounts count should be 0, got: %d", len(accounts))
	}

	if err := modelhelper.AddRelationship(&models.Relationship{
		Id:         bson.NewObjectId(),
		TargetId:   acc1.Id,
		TargetName: "JAccount",
		SourceId:   group.Id,
		SourceName: "JGroup",
		As:         "admin",
	}); err != nil {
		t.Error(err)
	}

	if err := modelhelper.AddRelationship(&models.Relationship{
		Id:         bson.NewObjectId(),
		TargetId:   acc2.Id,
		TargetName: "JAccount",
		SourceId:   group.Id,
		SourceName: "JGroup",
		As:         "admin",
	}); err != nil {
		t.Error(err)
	}

	accounts, err = modelhelper.FetchAdminAccounts(group.Slug)
	if err != nil {
		t.Error(err)
	}

	if len(accounts) != 2 {
		t.Errorf("accounts count should be 2, got: %d", len(accounts))
	}
}
Пример #2
0
func (c *cleaner) add(v interface{}) {
	var fn func()
	switch model := v.(type) {
	case *models.User:
		fn = func() {
			modelhelper.RemoveUser(model.Name)
		}
	case *models.Account:
		fn = func() {
			modelhelper.RemoveAccount(model.Id)
		}
	case *models.Group:
		fn = func() {
			modelhelper.RemoveGroup(model.Id)
		}
	case *models.Machine:
		fn = func() {
			modelhelper.DeleteMachine(model.ObjectId)
		}
	case *models.ComputeStack:
		fn = func() {
			modelhelper.DeleteComputeStack(model.Id.Hex())
		}
	default:
		panic(fmt.Errorf("cleaner for %T not implemented", v))
	}
	*c = append(*c, fn)
}
Пример #3
0
func TestUserLoginSessionCreation(t *testing.T) {
	db := modeltesthelper.NewMongoDB(t)
	defer db.Close()

	acc2 := createTestAccount(t)
	defer modelhelper.RemoveAccount(acc2.Id)

	group, err := createGroup()
	if err != nil {
		t.Error(err)
	}

	if err := modelhelper.AddRelationship(&models.Relationship{
		Id:         bson.NewObjectId(),
		TargetId:   acc2.Id,
		TargetName: "JAccount",
		SourceId:   group.Id,
		SourceName: "JGroup",
		As:         "member",
	}); err != nil {
		t.Error(err)
	}

	ses, err := modelhelper.UserLogin(acc2.Profile.Nickname, group.Slug)
	if err != nil {
		t.Errorf("expected nil error, but got %q!", err)
	}

	if ses == nil || ses.ClientId == "" {
		t.Error("expected ses.ClientId to be set, but got empty!")
	}
}
Пример #4
0
func TestGetAccountBySocialApiIds(t *testing.T) {
	db := modeltesthelper.NewMongoDB(t)
	defer db.Close()

	acc1 := createTestAccount(t)
	defer modelhelper.RemoveAccount(acc1.Id)

	acc2 := createTestAccount(t)
	defer modelhelper.RemoveAccount(acc2.Id)

	acc3 := createTestAccount(t)
	defer modelhelper.RemoveAccount(acc3.Id)

	accounts, err := modelhelper.GetAccountBySocialApiIds(acc1.SocialApiId, acc2.SocialApiId, acc3.SocialApiId)
	if err != nil {
		t.Error(err)
	}

	if len(accounts) != 3 {
		t.Errorf("accounts count should be 3, got: %d", len(accounts))
	}
}
Пример #5
0
func deleteAccount(res interface{}) error {
	acc := res.(*models.Account)
	if getUserByNick(acc.Profile.Nickname) {
		return nil
	}

	fmt.Printf("deleting acc %q\n", acc.Profile.Nickname)
	if !*flagDry {
		return helper.RemoveAccount(acc.Id)
	}

	return nil
}
Пример #6
0
func TestUserLogin(t *testing.T) {
	db := modeltesthelper.NewMongoDB(t)
	defer db.Close()

	acc1 := createTestAccount(t)
	defer modelhelper.RemoveAccount(acc1.Id)

	acc2 := createTestAccount(t)
	defer modelhelper.RemoveAccount(acc2.Id)

	group, err := createGroup()
	if err != nil {
		t.Error(err)
	}

	if err := modelhelper.AddRelationship(&models.Relationship{
		Id:         bson.NewObjectId(),
		TargetId:   acc1.Id,
		TargetName: "JAccount",
		SourceId:   group.Id,
		SourceName: "JGroup",
		As:         "member",
	}); err != nil {
		t.Error(err)
	}

	ses, err := modelhelper.CreateSessionForAccount(acc1.Profile.Nickname, group.Slug)
	if err != nil {
		t.Error(err)
	}

	tests := []struct {
		Title    string
		Nick     string
		Slug     string
		ClientID string
		Err      error
	}{
		{
			Title:    "Member account",
			Nick:     acc1.Profile.Nickname,
			Slug:     group.Slug,
			ClientID: ses.ClientId,
			Err:      nil,
		},
		{
			Title:    "Re-testing with Member account",
			Nick:     acc1.Profile.Nickname,
			Slug:     group.Slug,
			ClientID: ses.ClientId,
			Err:      nil,
		},
		{
			Title:    "Non-member account",
			Nick:     acc2.Profile.Nickname,
			Slug:     group.Slug,
			ClientID: "",
			Err:      modelhelper.ErrNotParticipant,
		},
	}

	for _, test := range tests {
		t.Run(test.Title, func(t *testing.T) {
			ses, err := modelhelper.UserLogin(test.Nick, test.Slug)
			if err != test.Err {
				t.Errorf("expected Err equal to %q, but got %q!", test.Err, err)
			}

			if ses != nil && ses.ClientId != test.ClientID {
				t.Errorf("expected ClientID equal to %q, but got %q!", test.ClientID, ses.ClientId)
			}
		})
	}
}
Пример #7
0
func TestFetchAccountGroups(t *testing.T) {
	db := modeltesthelper.NewMongoDB(t)
	defer db.Close()

	acc1 := createTestAccount(t)
	defer modelhelper.RemoveAccount(acc1.Id)

	groups, err := modelhelper.FetchAccountGroups(acc1.Profile.Nickname)
	if err != mgo.ErrNotFound {
		t.Fatalf("want err = %v; got %v", mgo.ErrNotFound, err)
	}

	if len(groups) != 0 {
		t.Fatalf("expected len(groups) to be 0, got groups: %+v", groups)
	}

	group1, err := createGroup()
	if err != nil {
		t.Fatal(err)
	}

	if err := modelhelper.AddRelationship(&models.Relationship{
		Id:         bson.NewObjectId(),
		TargetId:   acc1.Id,
		TargetName: "JAccount",
		SourceId:   group1.Id,
		SourceName: "JGroup",
		As:         "member",
	}); err != nil {
		t.Error(err)
	}

	groups, err = modelhelper.FetchAccountGroups(acc1.Profile.Nickname)
	if err != nil {
		t.Fatal(err)
	}

	if len(groups) != 1 {
		t.Fatalf("expected len(groups) to be 1, got groups: %+v", groups)
	}

	// test having 2 relationsips in one group
	group2, err := createGroup()
	if err != nil {
		t.Error(err)
	}

	if err := modelhelper.AddRelationship(&models.Relationship{
		Id:         bson.NewObjectId(),
		TargetId:   acc1.Id,
		TargetName: "JAccount",
		SourceId:   group2.Id,
		SourceName: "JGroup",
		As:         "admin",
	}); err != nil {
		t.Error(err)
	}

	if err := modelhelper.AddRelationship(&models.Relationship{
		Id:         bson.NewObjectId(),
		TargetId:   acc1.Id,
		TargetName: "JAccount",
		SourceId:   group2.Id,
		SourceName: "JGroup",
		As:         "member",
	}); err != nil {
		t.Fatal(err)
	}

	groups, err = modelhelper.FetchAccountGroups(acc1.Profile.Nickname)
	if err != nil {
		t.Fatal(err)
	}

	if len(groups) != 2 {
		t.Fatalf("expected len(groups) to be 2, got groups: %+v", groups)
	}
}
Пример #8
0
func TestHasRole(t *testing.T) {
	db := modeltesthelper.NewMongoDB(t)
	defer db.Close()

	acc1 := createTestAccount(t)
	defer modelhelper.RemoveAccount(acc1.Id)

	acc2 := createTestAccount(t)
	defer modelhelper.RemoveAccount(acc2.Id)

	group, err := createGroup()
	if err != nil {
		t.Error(err)
	}

	if err := modelhelper.AddRelationship(&models.Relationship{
		Id:         bson.NewObjectId(),
		TargetId:   acc1.Id,
		TargetName: "JAccount",
		SourceId:   group.Id,
		SourceName: "JGroup",
		As:         "member",
	}); err != nil {
		t.Error(err)
	}

	tests := []struct {
		Title string
		Nick  string
		Slug  string
		Roles []string
		Has   bool
	}{
		{
			Title: "Member account",
			Nick:  acc1.Profile.Nickname,
			Slug:  group.Slug,
			Roles: []string{"member"},
			Has:   true,
		},
		{
			Title: "Member account with multi role",
			Nick:  acc1.Profile.Nickname,
			Slug:  group.Slug,
			Roles: []string{"member", "admin"},
			Has:   true,
		},
		{
			Title: "Member account with default roles",
			Nick:  acc1.Profile.Nickname,
			Slug:  group.Slug,
			Roles: modelhelper.DefaultRoles,
			Has:   true,
		},
		{
			Title: "Non-member account",
			Nick:  acc2.Profile.Nickname,
			Slug:  group.Slug,
			Roles: []string{"member"},
			Has:   false,
		},
		{
			Title: "Invalid role correct account",
			Nick:  acc1.Profile.Nickname,
			Slug:  group.Slug,
			Roles: []string{"admin"},
			Has:   false,
		},
		{
			Title: "Invalid role in-correct account",
			Nick:  acc2.Profile.Nickname,
			Slug:  group.Slug,
			Roles: []string{"admin"},
			Has:   false,
		},
	}

	for _, test := range tests {
		has, err := modelhelper.HasAnyRole(test.Nick, test.Slug, test.Roles...)
		if err != nil {
			t.Error(err)
		}
		if has != test.Has {
			t.Errorf("expected %q's \"has\" equal to %t, but it wasnt!", test.Title, test.Has)
		}
	}
}