Ejemplo n.º 1
0
        public async Task UpdateGroupMembership_ValidResponse()
        {
            Handler.Setup(h => h.ExecuteAsync <BoxGroupMembership>(It.IsAny <IBoxRequest>()))
            .Returns(() => Task.FromResult <IBoxResponse <BoxGroupMembership> >(new BoxResponse <BoxGroupMembership>()
            {
                Status        = ResponseStatus.Success,
                ContentString = @"{ ""type"": ""group_membership"", ""id"": ""2381570"",
                                        ""user"": { ""type"": ""user"", ""id"": ""4940223"", ""name"": ""<script>alert(12);</script>"", ""login"": ""*****@*****.**""},
                                        ""group"": { ""type"": ""group"", ""id"": ""159622"", ""name"": ""test204bbee0-f3b0-43b4-b213-214fcd2cb9a7"" },
                                        ""role"": ""admin"",
                                        ""created_at"": ""2013-11-13T13:19:44-08:00"",
                                        ""modified_at"": ""2013-11-13T13:19:44-08:00""}"
            }));

            BoxGroupMembershipRequest request = new BoxGroupMembershipRequest()
            {
                Role = "anything"
            };

            BoxGroupMembership response = await _groupsManager.UpdateGroupMembershipAsync("123", request);

            Assert.AreEqual("2381570", response.Id, "Wrong Membership id");
            Assert.AreEqual("group_membership", response.Type, "wrong type");
            Assert.AreEqual("159622", response.Group.Id, "Wrong group id");
            Assert.AreEqual("4940223", response.User.Id, "Wrong user id");
            Assert.AreEqual("admin", response.Role, "Wrong role");
            Assert.AreEqual(DateTime.Parse("2013-11-13T13:19:44-08:00"), response.CreatedAt, "Wrong created at");
            Assert.AreEqual(DateTime.Parse("2013-11-13T13:19:44-08:00"), response.ModifiedAt, "Wrong modified at");
        }
Ejemplo n.º 2
0
        private async Task RunCreate()
        {
            base.CheckForValue(this._membershipId.Value, this._app, "A group memebership ID is required for this command.");
            var role = "";

            if (this._admin.HasValue())
            {
                role = "admin";
            }
            else if (this._member.HasValue())
            {
                role = "member";
            }
            var boxClient     = base.ConfigureBoxClient(oneCallAsUserId: base._asUser.Value(), oneCallWithToken: base._oneUseToken.Value());
            var memberRequest = new BoxGroupMembershipRequest();

            if (!string.IsNullOrEmpty(role))
            {
                memberRequest.Role = role;
            }
            else
            {
                throw new Exception("Couldn't update the user's membership role.");
            }
            var updatedMembership = await boxClient.GroupsManager.UpdateGroupMembershipAsync(this._membershipId.Value, memberRequest);

            if (base._json.HasValue() || this._home.GetBoxHomeSettings().GetOutputJsonSetting())
            {
                base.OutputJson(updatedMembership);
                return;
            }
            Reporter.WriteSuccess("Updated the user's role.");
            base.PrintGroupMember(updatedMembership);
        }
Ejemplo n.º 3
0
        private async Task RunCreate()
        {
            base.CheckForValue(this._userId.Value, this._app, "A user ID is required for this command.");
            base.CheckForValue(this._groupId.Value, this._app, "A group ID is required for this command.");
            var role = "";

            if (this._admin.HasValue())
            {
                role = "admin";
            }
            else
            {
                role = "member";
            }
            var boxClient     = base.ConfigureBoxClient(oneCallAsUserId: base._asUser.Value(), oneCallWithToken: base._oneUseToken.Value());
            var memberRequest = new BoxGroupMembershipRequest();

            memberRequest.Group    = new BoxGroupRequest();
            memberRequest.Group.Id = this._groupId.Value;
            memberRequest.User     = new BoxRequestEntity();
            memberRequest.User.Id  = this._userId.Value;
            memberRequest.Role     = role;
            var membership = await boxClient.GroupsManager.AddMemberToGroupAsync(memberRequest);

            if (base._json.HasValue() || this._home.GetBoxHomeSettings().GetOutputJsonSetting())
            {
                base.OutputJson(membership);
                return;
            }
            base.PrintGroupMember(membership);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Add a user to a group.
        /// </summary>
        /// <param name="membershipRequest">BoxGroupMembershipRequest object.</param>
        /// <param name="fields">Attribute(s) to include in the response.</param>
        /// <returns>The group membership created.</returns>
        public async Task <BoxGroupMembership> AddMemberToGroupAsync(BoxGroupMembershipRequest membershipRequest, IEnumerable <string> fields = null)
        {
            membershipRequest.ThrowIfNull("membershipRequest")
            .Group.Id.ThrowIfNullOrWhiteSpace("Group.Id");
            membershipRequest.User.Id.ThrowIfNullOrWhiteSpace("User.Id");

            BoxRequest request = new BoxRequest(_config.GroupMembershipEndpointUri)
                                 .Method(RequestMethod.Post)
                                 .Param(ParamFields, fields)
                                 .Payload(_converter.Serialize <BoxGroupMembershipRequest>(membershipRequest));

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

            return(response.ResponseObject);
        }
        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);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Updates the specified group membership with the specified request
        /// </summary>
        /// <param name="membershipId">It of the group membership to update</param>
        /// <param name="memRequest">The request specifying the update</param>
        /// <param name="fields">Optional fields to return</param>
        /// <returns>The updated group membership</returns>
        public async Task <BoxGroupMembership> UpdateGroupMembershipAsync(string membershipId, BoxGroupMembershipRequest memRequest, List <string> fields = null)
        {
            membershipId.ThrowIfNullOrWhiteSpace("membershipId");
            memRequest.ThrowIfNull("memRequest").Role.ThrowIfNullOrWhiteSpace("role");

            BoxRequest request = new BoxRequest(_config.GroupMembershipEndpointUri, membershipId)
                                 .Param(ParamFields, fields)
                                 .Method(RequestMethod.Put)
                                 .Payload(_converter.Serialize <BoxGroupMembershipRequest>(memRequest));

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

            return(response.ResponseObject);
        }
Ejemplo n.º 7
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);
        }