예제 #1
0
        private async Task RunUpdate()
        {
            base.CheckForValue(this._id.Value, this._app, "A group ID is required for this command");
            var boxClient    = base.ConfigureBoxClient(oneCallAsUserId: base._asUser.Value(), oneCallWithToken: base._oneUseToken.Value());
            var groupRequest = new BoxGroupRequest();

            if (this._name.HasValue())
            {
                groupRequest.Name = this._name.Value();
            }
            if (this._inviteLevel.HasValue())
            {
                groupRequest.InvitabilityLevel = base.CheckInvitabilityLevel(this._inviteLevel.Value());
            }
            if (this._viewMembershipLevel.HasValue())
            {
                groupRequest.MemberViewabilityLevel = base.CheckViewMembersLevel(this._viewMembershipLevel.Value());
            }
            var updatedGroup = await boxClient.GroupsManager.UpdateAsync(this._id.Value, groupRequest);

            if (base._json.HasValue() || this._home.GetBoxHomeSettings().GetOutputJsonSetting())
            {
                base.OutputJson(updatedGroup);
                return;
            }
            base.PrintGroup(updatedGroup);
        }
        public async Task GroupsWorkflow_ValidRequest_GetGroups()
        {
            // Get all groups and one individual group
            var allGroupsInit = await _client.GroupsManager.GetAllGroupsAsync();

            var oneGroup = await _client.GroupsManager.GetGroupAsync(allGroupsInit.Entries[0].Id);

            Assert.AreEqual(allGroupsInit.Entries[0].Name, oneGroup.Name, "Did not retrieve the correct group");

            // Create a new group
            string groupName = GetUniqueName();

            BoxGroupRequest groupReq = new BoxGroupRequest()
            {
                Name = groupName,
                InvitabilityLevel      = RequestParameters.AdminsOnly,
                MemberViewabilityLevel = RequestParameters.AdminsOnly
            };

            var newGroup = await _client.GroupsManager.CreateAsync(groupReq);

            var allGroupsAfterAdd = await _client.GroupsManager.GetAllGroupsAsync(limit : 3, autoPaginate : true);

            Assert.AreEqual(newGroup.Name, groupName, "New group does not have correct name");
            Assert.AreEqual(allGroupsInit.TotalCount + 1, allGroupsAfterAdd.TotalCount, "Number of groups after add is not correct");

            //Update the name of an existing group
            string updatedName = GetUniqueName();

            Assert.IsFalse(allGroupsInit.Entries.Any(x => x.Name.Equals(updatedName)), "A group with updatedName already exists");

            BoxGroupRequest updateRequest = new BoxGroupRequest()
            {
                Name = updatedName
            };

            var updatedGroup = await _client.GroupsManager.UpdateAsync(newGroup.Id, updateRequest);

            var allGroupsAfterUpdate = await _client.GroupsManager.GetAllGroupsAsync();

            Assert.AreEqual(updatedGroup.Name, updatedName, "The group name was not updated correctly");
            Assert.AreEqual(1, allGroupsAfterUpdate.Entries.Count(x => x.Name.Equals(updatedName)), "The updated group name does not exist among all groups");
            Assert.IsFalse(allGroupsAfterUpdate.Entries.Any(x => x.Name.Equals(groupName)), "The old group name still exists among all groups");

            // Delete a group
            var delResult = await _client.GroupsManager.DeleteAsync(newGroup.Id);

            var allGroupsAfterDelete = await _client.GroupsManager.GetAllGroupsAsync();

            Assert.IsTrue(delResult, "Group was not deleted successfully");
            Assert.AreEqual(allGroupsInit.TotalCount, allGroupsAfterDelete.TotalCount, "Number of groups after delete is not correct");
            Assert.IsFalse(allGroupsAfterDelete.Entries.Any(x => x.Id == newGroup.Id), "Deleted group still exists");
        }
예제 #3
0
        private async Task RunCreate()
        {
            if (this._bulkPath.HasValue())
            {
                var json = false;
                if (base._json.HasValue() || this._home.GetBoxHomeSettings().GetOutputJsonSetting())
                {
                    json = true;
                }
                await base.CreateGroupsFromFile(this._bulkPath.Value(), this._save.HasValue(),
                                                json : json, overrideSaveFileFormat : this._fileFormat.Value());

                return;
            }
            base.CheckForValue(this._name.Value, this._app, "A group name is required for this command");
            var boxClient    = base.ConfigureBoxClient(oneCallAsUserId: base._asUser.Value(), oneCallWithToken: base._oneUseToken.Value());
            var groupRequest = new BoxGroupRequest();

            groupRequest.Name = this._name.Value;
            if (this._inviteLevel.HasValue())
            {
                groupRequest.InvitabilityLevel = base.CheckInvitabilityLevel(this._inviteLevel.Value());
            }
            if (this._viewMembershipLevel.HasValue())
            {
                groupRequest.MemberViewabilityLevel = base.CheckViewMembersLevel(this._viewMembershipLevel.Value());
            }
            var createdGroup = await boxClient.GroupsManager.CreateAsync(groupRequest);

            if (_save.HasValue())
            {
                var fileName = $"{base._names.CommandNames.Groups}-{base._names.SubCommandNames.Create}-{DateTime.Now.ToString(GeneralUtilities.GetDateFormatString())}";
                Reporter.WriteInformation("Saving file...");
                var listWrapper = new List <BoxGroup>();
                listWrapper.Add(createdGroup);
                var saved = base.WriteListResultsToReport <BoxGroup, BoxGroupMap>(listWrapper, fileName, fileFormat: this._fileFormat.Value());
                Reporter.WriteInformation($"File saved: {saved}");
                return;
            }
            if (this._idOnly.HasValue())
            {
                Reporter.WriteInformation(createdGroup.Id);
                return;
            }
            if (base._json.HasValue() || this._home.GetBoxHomeSettings().GetOutputJsonSetting())
            {
                base.OutputJson(createdGroup);
                return;
            }
            base.PrintGroup(createdGroup);
        }
        /// <summary>
        /// Create a new group.
        /// </summary>
        /// <param name="groupRequest">BoxGroupRequest object.</param>
        /// <param name="fields">Attribute(s) to include in the response.</param>
        /// <returns>The newly created group.</returns>
        public async Task <BoxGroup> CreateAsync(BoxGroupRequest groupRequest, IEnumerable <string> fields = null)
        {
            groupRequest.ThrowIfNull("groupRequest")
            .Name.ThrowIfNullOrWhiteSpace("groupRequest.Name");

            BoxRequest request = new BoxRequest(_config.GroupsEndpointUri)
                                 .Param(ParamFields, fields)
                                 .Method(RequestMethod.Post)
                                 .Payload(_converter.Serialize <BoxGroupRequest>(groupRequest));

            IBoxResponse <BoxGroup> response = await ToResponseAsync <BoxGroup>(request).ConfigureAwait(false);

            return(response.ResponseObject);
        }
        public async Task UpdateGroup_ExtraFields_ValidGroup()
        {
            IBoxRequest boxRequest = null;

            Handler.Setup(h => h.ExecuteAsync <BoxGroup>(It.IsAny <IBoxRequest>()))
            .Returns(() => Task.FromResult <IBoxResponse <BoxGroup> >(new BoxResponse <BoxGroup>()
            {
                Status        = ResponseStatus.Success,
                ContentString = @"{
                        ""type"": ""group"",
                        ""id"": ""159322"",
                        ""description"": ""A group from Okta"",
                        ""external_sync_identifier"": ""foo"",
                        ""provenance"": ""Okta"",
                        ""invitability_level"": ""admins_only"",
                        ""member_viewability_level"": ""admins_only""
                    }"
            }))
            .Callback <IBoxRequest>(r => boxRequest = r);

            BoxGroupRequest request = new BoxGroupRequest()
            {
                Description            = "A group from Okta",
                ExternalSyncIdentifier = "foo",
                Provenance             = "Okta",
                InvitabilityLevel      = "admins_only",
                MemberViewabilityLevel = "admins_only"
            };

            var fields = new string[]
            {
                BoxGroup.FieldDescription,
                BoxGroup.FieldExternalSyncIdentifier,
                BoxGroup.FieldProvenance,
                BoxGroup.FieldInvitabilityLevel,
                BoxGroup.FieldMemberViewabilityLevel
            };

            BoxGroup group = await _groupsManager.UpdateAsync("123", request, fields : fields);

            Assert.AreEqual("{\"description\":\"A group from Okta\",\"provenance\":\"Okta\",\"external_sync_identifier\":\"foo\",\"invitability_level\":\"admins_only\",\"member_viewability_level\":\"admins_only\"}", boxRequest.Payload);
            Assert.AreEqual("A group from Okta", group.Description);
            Assert.AreEqual("foo", group.ExternalSyncIdentifier);
            Assert.AreEqual("Okta", group.Provenance);
            Assert.AreEqual("admins_only", group.InvitabilityLevel);
            Assert.AreEqual("admins_only", group.MemberViewabilityLevel);
        }
예제 #6
0
        public async Task CreateGroup_ValidResponse_NewGroup()
        {
            Handler.Setup(h => h.ExecuteAsync <BoxGroup>(It.IsAny <BoxRequest>()))
            .Returns(() => Task.FromResult <IBoxResponse <BoxGroup> >(new BoxResponse <BoxGroup>()
            {
                Status        = ResponseStatus.Success,
                ContentString = @"{ ""type"": ""group"", ""id"": ""159322"", ""name"": ""TestGroup2"", ""created_at"": ""2013-11-12T15:19:47-08:00"", ""modified_at"": ""2013-11-12T15:19:47-08:00"" }"
            }));

            BoxGroupRequest request = new BoxGroupRequest()
            {
                Name = "NewGroup"
            };

            BoxGroup group = await _groupsManager.CreateAsync(request);

            Assert.AreEqual <string>("TestGroup2", group.Name, "Wrong group name");
            Assert.AreEqual <string>("159322", group.Id, "Wrong id");
            Assert.AreEqual(DateTime.Parse("2013-11-12T15:19:47-08:00"), group.ModifiedAt, "Wrong modified at");
            Assert.AreEqual(DateTime.Parse("2013-11-12T15:19:47-08:00"), group.CreatedAt, "Wrong created at");
        }
예제 #7
0
        private async Task RunCreate()
        {
            if (this._bulkPath.HasValue())
            {
                var json = false;
                if (base._json.HasValue() || this._home.GetBoxHomeSettings().GetOutputJsonSetting())
                {
                    json = true;
                }
                await base.CreateGroupsFromFile(this._bulkPath.Value(), this._save.HasValue(), json : json);

                return;
            }
            base.CheckForValue(this._name.Value, this._app, "A group name is required for this command");
            var boxClient    = base.ConfigureBoxClient(oneCallAsUserId: base._asUser.Value(), oneCallWithToken: base._oneUseToken.Value());
            var groupRequest = new BoxGroupRequest();

            groupRequest.Name = this._name.Value;
            if (this._inviteLevel.HasValue())
            {
                groupRequest.InvitabilityLevel = base.CheckInvitabilityLevel(this._inviteLevel.Value());
            }
            if (this._viewMembershipLevel.HasValue())
            {
                groupRequest.MemberViewabilityLevel = base.CheckViewMembersLevel(this._viewMembershipLevel.Value());
            }
            var createdGroup = await boxClient.GroupsManager.CreateAsync(groupRequest);

            if (this._idOnly.HasValue())
            {
                Reporter.WriteInformation(createdGroup.Id);
                return;
            }
            if (base._json.HasValue() || this._home.GetBoxHomeSettings().GetOutputJsonSetting())
            {
                base.OutputJson(createdGroup);
                return;
            }
            base.PrintGroup(createdGroup);
        }
        public async Task GroupMembershipWorkflow_ValidRequest()
        {
            // Get current user
            var user = await _client.UsersManager.GetCurrentUserInformationAsync();

            // Get all the current memberships for this user
            var current_memberships = await _client.GroupsManager.GetAllGroupMembershipsForUserAsync(user.Id);

            // Create a new group
            string groupName = GetUniqueName();

            BoxGroupRequest groupReq = new BoxGroupRequest()
            {
                Name = groupName,
            };

            var newGroup = await _client.GroupsManager.CreateAsync(groupReq);

            // Create a membership
            BoxGroupMembershipRequest request = new BoxGroupMembershipRequest()
            {
                User = new BoxRequestEntity()
                {
                    Id = user.Id
                },
                Group = new BoxGroupRequest()
                {
                    Id = newGroup.Id
                }
            };

            var responseMembership = await _client.GroupsManager.AddMemberToGroupAsync(request);

            Assert.AreEqual("group_membership", responseMembership.Type, "The type is not group_membership");
            Assert.AreEqual("member", responseMembership.Role, "Membership role is not set correctly");
            Assert.AreEqual(user.Id, responseMembership.User.Id, "User id not set correctly for membership");
            Assert.AreEqual(newGroup.Id, responseMembership.Group.Id, "Group id not set correctly for membership");

            // Get the created group membership
            var membership = await _client.GroupsManager.GetGroupMembershipAsync(responseMembership.Id);

            Assert.AreEqual("group_membership", membership.Type, "The type is not group_membership");
            Assert.AreEqual("member", membership.Role, "Membership role is not set correctly");
            Assert.AreEqual(user.Id, membership.User.Id, "User id not set correctly for membership");
            Assert.AreEqual(newGroup.Id, membership.Group.Id, "Group id not set correctly for membership");

            // Update the group membership's role
            request = new BoxGroupMembershipRequest()
            {
                Role = "admin"
            };
            var updatedMembership = await _client.GroupsManager.UpdateGroupMembershipAsync(responseMembership.Id, request);

            Assert.AreEqual("admin", updatedMembership.Role, "Membership role was not updated correctly");

            // Get all memberships for the given groups
            var memberships = await _client.GroupsManager.GetAllGroupMembershipsForGroupAsync(newGroup.Id);

            Assert.AreEqual(1, memberships.Entries.Count, "Wrong count of memberships");
            Assert.AreEqual(1, memberships.TotalCount, "Wrong total count of memberships");
            Assert.AreEqual("group_membership", memberships.Entries[0].Type, "Wrong type");
            Assert.AreEqual(newGroup.Id, memberships.Entries[0].Group.Id, "Wrong Group id");
            Assert.AreEqual(user.Id, memberships.Entries[0].User.Id, "Wrong User id");

            // Add this group to a folder
            const string folderId = "1927307787";

            // Add Collaboration
            BoxCollaborationRequest addRequest = new BoxCollaborationRequest()
            {
                Item = new BoxRequestEntity()
                {
                    Id   = folderId,
                    Type = BoxType.folder
                },
                AccessibleBy = new BoxCollaborationUserRequest()
                {
                    Type = BoxType.group,
                    Id   = newGroup.Id
                },
                Role = "viewer"
            };

            BoxCollaboration collab = await _client.CollaborationsManager.AddCollaborationAsync(addRequest, notify : false);

            Assert.AreEqual(folderId, collab.Item.Id, "Folder and collaboration folder id do not match");
            Assert.AreEqual(BoxCollaborationRoles.Viewer, collab.Role, "Incorrect collaboration role");

            // Get all collaborations for the give group
            var collabs = await _client.GroupsManager.GetCollaborationsForGroupAsync(newGroup.Id);

            Assert.AreEqual(1, collabs.Entries.Count, "Wrong count of collaborations");
            Assert.AreEqual(1, collabs.TotalCount, "Wrong total count of collaborations");

            collab = collabs.Entries[0];
            Assert.AreEqual <string>(newGroup.Id, collab.AccessibleBy.Id, "Wrong Group Id");
            Assert.AreEqual <string>("viewer", collab.Role, "Wrong Role Type");

            // Get memberships for the user
            memberships = await _client.GroupsManager.GetAllGroupMembershipsForUserAsync(user.Id);

            Assert.AreEqual(current_memberships.TotalCount + 1, memberships.TotalCount, "The total count of memberships for user did not increase");
            Assert.IsTrue(memberships.Entries.Exists(m => m.Id.Equals(membership.Id)), "Newly created group membership does not exist in this users list of memberships");

            // Delete the group membership
            bool success = await _client.GroupsManager.DeleteGroupMembershipAsync(membership.Id);

            memberships = await _client.GroupsManager.GetAllGroupMembershipsForGroupAsync(newGroup.Id);

            Assert.AreEqual(0, memberships.Entries.Count, "Count should be 0");
            Assert.AreEqual(0, memberships.TotalCount, "Total count should be 0");

            // Clean up - delete group
            var delResult = await _client.GroupsManager.DeleteAsync(newGroup.Id);
        }
예제 #9
0
        public async Task GroupMembershipWorkflow_ValidRequest()
        {
            // Get current user
            var user = await _client.UsersManager.GetCurrentUserInformationAsync();

            // Get all the current memberships for this user
            var current_memberships = await _client.GroupsManager.GetAllGroupMembershipsForUserAsync(user.Id);

            // Create a new group
            string groupName = GetUniqueName();

            BoxGroupRequest groupReq = new BoxGroupRequest()
            {
                Name = groupName,
            };

            var newGroup = await _client.GroupsManager.CreateAsync(groupReq);

            // Create a membership
            BoxGroupMembershipRequest request = new BoxGroupMembershipRequest()
            {
                User = new BoxRequestEntity()
                {
                    Id = user.Id
                },
                Group = new BoxGroupRequest()
                {
                    Id = newGroup.Id
                }
            };

            var responseMembership = await _client.GroupsManager.AddMemberToGroupAsync(request);

            Assert.AreEqual <string>("group_membership", responseMembership.Type, "The type is not group_membership");
            Assert.AreEqual <string>("member", responseMembership.Role, "Membership role is not set correctly");
            Assert.AreEqual <string>(user.Id, responseMembership.User.Id, "User id not set correctly for membership");
            Assert.AreEqual <string>(newGroup.Id, responseMembership.Group.Id, "Group id not set correctly for membership");

            // Get the created group membership
            var membership = await _client.GroupsManager.GetGroupMembershipAsync(responseMembership.Id);

            Assert.AreEqual <string>("group_membership", membership.Type, "The type is not group_membership");
            Assert.AreEqual <string>("member", membership.Role, "Membership role is not set correctly");
            Assert.AreEqual <string>(user.Id, membership.User.Id, "User id not set correctly for membership");
            Assert.AreEqual <string>(newGroup.Id, membership.Group.Id, "Group id not set correctly for membership");

            // Update the group membership's role
            request = new BoxGroupMembershipRequest()
            {
                Role = "admin"
            };
            var updatedMembership = await _client.GroupsManager.UpdateGroupMembershipAsync(responseMembership.Id, request);

            Assert.AreEqual <string>("admin", updatedMembership.Role, "Membership role was not updated correctly");

            // Get all memberships for the given groups
            var memberships = await _client.GroupsManager.GetAllGroupMembershipsForGroupAsync(newGroup.Id);

            Assert.AreEqual <int>(1, memberships.Entries.Count, "Wrong count of memberships");
            Assert.AreEqual <int>(1, memberships.TotalCount, "Wrong total count of memberships");
            Assert.AreEqual <string>("group_membership", memberships.Entries[0].Type, "Wrong type");
            Assert.AreEqual <string>(newGroup.Id, memberships.Entries[0].Group.Id, "Wrong Group id");
            Assert.AreEqual <string>(user.Id, memberships.Entries[0].User.Id, "Wrong User id");

            // Get memberships for the user
            memberships = await _client.GroupsManager.GetAllGroupMembershipsForUserAsync(user.Id);

            Assert.AreEqual <int>(current_memberships.TotalCount + 1, memberships.TotalCount, "The total count of memberships for user did not increase");
            Assert.IsTrue(memberships.Entries.Exists(m => m.Id.Equals(membership.Id)), "Newly created group membership does not exist in this users list of memberships");

            // Delete the group membership
            bool success = await _client.GroupsManager.DeleteGroupMembershipAsync(membership.Id);

            memberships = await _client.GroupsManager.GetAllGroupMembershipsForGroupAsync(newGroup.Id);

            Assert.AreEqual <int>(0, memberships.Entries.Count, "Count should be 0");
            Assert.AreEqual <int>(0, memberships.TotalCount, "Total count should be 0");

            // Clean up - delete group
            var delResult = await _client.GroupsManager.DeleteAsync(newGroup.Id);
        }