Exemplo n.º 1
0
func TestGetMostRecentSession(t *testing.T) {
	db := modeltesthelper.NewMongoDB(t)
	defer db.Close()
	g, err := createGroup()
	if err != nil {
		t.Fatalf("createGroup()=%s", err)
	}

	newSession := func(long time.Duration) *models.Session {
		return &models.Session{
			Id:           bson.NewObjectId(),
			ClientId:     bson.NewObjectId().Hex(),
			Username:     "******",
			GroupName:    g.Slug,
			SessionBegan: time.Now().UTC().Add(long),
			LastAccess:   time.Now().UTC().Add(long),
		}
	}

	sessions := []*models.Session{
		newSession(0),
		newSession(10 * time.Minute),
		newSession(1 * time.Hour), // <- this is most recent
		newSession(15 * time.Second),
		newSession(58 * time.Minute),
		newSession(-1 * time.Hour),
	}

	for i, s := range sessions {
		if err := modelhelper.CreateSession(s); err != nil {
			t.Fatalf("%d: CreateSession()=%s", i, err)
		}
	}

	got, err := modelhelper.GetMostRecentSession("user")
	if err != nil {
		t.Fatalf("GetMostRecentSession()=%s", err)
	}

	if got.Id != sessions[2].Id {
		t.Fatalf("got %q, want %q", got.Id.Hex(), sessions[2].Id.Hex())
	}
}
Exemplo n.º 2
0
func TestLookupGroup(t *testing.T) {
	const N = 10

	db := modeltesthelper.NewMongoDB(t)
	defer db.Close()

	user := &models.User{
		ObjectId: bson.NewObjectId(),
		Name:     bson.NewObjectId().Hex(),
		Email:    bson.NewObjectId().Hex(),
	}

	if err := modelhelper.CreateUser(user); err != nil {
		t.Fatalf("CreateUser()=%s", err)
	}

	groups, err := createGroups(N + 1)
	if err != nil {
		t.Fatalf("createGroups()=%s", err)
	}

	machines, err := createMachines(N, t)
	if err != nil {
		t.Fatalf("createMachines()=%s", err)
	}

	for i := range machines {
		machines[i].Groups = []models.MachineGroup{{Id: groups[i].Id}}

		update := bson.M{
			"$set": bson.M{
				"groups": machines[i].Groups,
				"users": []*models.MachineUser{{
					Id:       user.ObjectId,
					Username: user.Name,
				}},
			},
		}

		if i&2 == 0 {
			// force to lookup by registerUrl for machines with even index
			update["$set"].(bson.M)["ipAddress"] = ""
		}

		err := modelhelper.UpdateMachine(machines[i].ObjectId, update)
		if err != nil {
			t.Fatalf("UpdateMachine()=%s", err)
		}
	}

	session := &models.Session{
		Id:        bson.NewObjectId(),
		GroupName: groups[N].Slug,
		Username:  user.Name,
	}

	if err := modelhelper.CreateSession(session); err != nil {
		t.Fatalf("CreateSession()=%s")
	}

	cases := map[string]struct {
		opts *modelhelper.LookupGroupOptions
		id   bson.ObjectId
	}{
		"lookup by queryString": {
			&modelhelper.LookupGroupOptions{
				Username: user.Name,
				KiteID:   mustKiteID(machines[0].QueryString),
			},
			groups[0].Id,
		},
		"lookup by ipAddress": {
			&modelhelper.LookupGroupOptions{
				Username:  user.Name,
				ClientURL: machines[1].RegisterURL,
			},
			groups[1].Id,
		},
		"lookup by registerUrl": {
			&modelhelper.LookupGroupOptions{
				Username:  user.Name,
				ClientURL: machines[4].RegisterURL,
			},
			groups[4].Id,
		},
		"lookup by most recent session for KD": {
			&modelhelper.LookupGroupOptions{
				Username:    user.Name,
				Environment: "managed",
			},
			groups[N].Id,
		},
	}

	for name, cas := range cases {
		t.Run(name, func(t *testing.T) {
			team, err := modelhelper.LookupGroup(cas.opts)
			if err != nil {
				t.Fatalf("LookupGroup()=%s", err)
			}

			if team.Id != cas.id {
				t.Fatalf("got %q, want %q", team.Id.Hex(), cas.id.Hex())
			}
		})
	}
}