Esempio n. 1
0
// TestWhoami is a kite handler for a "team.whoami" kite method.
func (k *Kloud) TeamWhoami(r *kite.Request) (interface{}, error) {
	opts := &modelhelper.LookupGroupOptions{
		Username:    r.Username,
		KiteID:      r.Client.ID,
		ClientURL:   r.Client.URL,
		Environment: r.Client.Environment,
	}

	group, err := modelhelper.LookupGroup(opts)
	if err != nil {
		return nil, models.ResError(err, "jGroup")
	}

	return &WhoamiResponse{
		Team: &team.Team{
			Name:      group.Title,
			Slug:      group.Slug,
			Privacy:   group.Privacy,
			SubStatus: group.Payment.Subscription.Status,
		},
	}, nil
}
Esempio 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())
			}
		})
	}
}