Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Unverified Commit c3c12a10 authored by Andy Scherzinger's avatar Andy Scherzinger Committed by GitHub
Browse files

Merge pull request #522 from nextcloud/longNote

Escape note and label when updating share
parents 89dbef4c 89558e45
Loading
Loading
Loading
Loading
+120 −0
Original line number Diff line number Diff line
/*
 * Nextcloud Android client application
 *
 * @author Tobias Kaminsky
 * Copyright (C) 2020 Tobias Kaminsky
 * Copyright (C) 2020 Nextcloud GmbH
 *  
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU Affero General Public License for more details.
 *  
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program. If not, see <https://www.gnu.org/licenses/>.
 *  
 */
package com.owncloud.android.lib.resources.shares

import com.owncloud.android.AbstractIT
import com.owncloud.android.lib.resources.files.CreateFolderRemoteOperation
import com.owncloud.android.lib.resources.files.RemoveFileRemoteOperation
import junit.framework.Assert.assertEquals
import junit.framework.Assert.assertTrue
import org.junit.Assert
import org.junit.Test

class UpdateShareRemoteOperationIT : AbstractIT() {
    @Test
    fun shortNote() {
        testUpdateNote("123")
    }

    @Test
    fun middleNote() {
        testUpdateNote("123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123")
    }

    @Test
    fun longNote() {
        testUpdateNote("123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123" +
                "123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123" +
                "123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123" +
                "123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123")
    }

    @Test
    fun testEscapedNote() {
        testUpdateNote("test & test")
    }

    private fun testUpdateNote(note: String) {
        Assert.assertTrue(CreateFolderRemoteOperation("/note/", true).execute(client).isSuccess)

        // share folder to user "admin"
        val createOperationResult = CreateShareRemoteOperation("/note/",
                ShareType.USER,
                "admin",
                false,
                "",
                OCShare.MAXIMUM_PERMISSIONS_FOR_FOLDER,
                true).execute(client)

        assertTrue(createOperationResult.isSuccess)

        val share = createOperationResult.data[0] as OCShare

        val sut = UpdateShareRemoteOperation(share.remoteId)
        sut.setNote(note)

        assertTrue(sut.execute(client).isSuccess)

        // verify
        val getShareOperationResult = GetShareRemoteOperation(share.remoteId).execute(client)
        assertTrue(getShareOperationResult.isSuccess)

        val updatedShare = getShareOperationResult.data[0] as OCShare

        assertEquals(note, updatedShare.note)

        assertTrue(RemoveFileRemoteOperation("/note/").execute(client).isSuccess)
    }

    @Test
    fun updateLabel() {
        val label = "test & test"
        Assert.assertTrue(CreateFolderRemoteOperation("/label/", true).execute(client).isSuccess)

        // share folder via public link
        val createOperationResult = CreateShareRemoteOperation("/label/",
                ShareType.PUBLIC_LINK,
                "",
                true,
                "",
                OCShare.READ_PERMISSION_FLAG).execute(client)

        assertTrue(createOperationResult.isSuccess)

        val share = createOperationResult.data[0] as OCShare

        val sut = UpdateShareRemoteOperation(share.remoteId)
        sut.setLabel(label)

        assertTrue(sut.execute(client).isSuccess)

        // verify
        val getShareOperationResult = GetShareRemoteOperation(share.remoteId).execute(client)
        assertTrue(getShareOperationResult.isSuccess)

        val updatedShare = getShareOperationResult.data[0] as OCShare

        assertEquals(label, updatedShare.label)

        assertTrue(RemoveFileRemoteOperation("/label/").execute(client).isSuccess)
    }
}
+20 −8
Original line number Diff line number Diff line
@@ -67,6 +67,7 @@ public class CreateShareRemoteOperation extends RemoteOperation {
     *                       If true public can upload to a shared folder. Only available for public link shares
     * @param password       Password to protect a public link share. Only available for public link shares
     * @param permissions    1 - Read only Default for public shares
     * @param getShareDetails if true return share info
     *                       2 - Update
     *                       4 - Create
     *                       8 - Delete
@@ -82,7 +83,8 @@ public class CreateShareRemoteOperation extends RemoteOperation {
            String shareWith,
            boolean publicUpload,
            String password,
        int permissions
            int permissions,
            boolean getShareDetails
                                     ) {
        mRemoteFilePath = remoteFilePath;
        mShareType = shareType;
@@ -90,7 +92,17 @@ public class CreateShareRemoteOperation extends RemoteOperation {
        mPublicUpload = publicUpload;
        mPassword = password;
        mPermissions = permissions;
        mGetShareDetails = false;        // defaults to false for backwards compatibility
        mGetShareDetails = getShareDetails;        // defaults to false for backwards compatibility
    }

    public CreateShareRemoteOperation(
            String remoteFilePath,
            ShareType shareType,
            String shareWith,
            boolean publicUpload,
            String password,
            int permissions) {
        this(remoteFilePath, shareType, shareWith, publicUpload, password, permissions, false);
    }

    public boolean isGettingShareDetails() {
+3 −2
Original line number Diff line number Diff line
@@ -37,6 +37,7 @@ import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PutMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;

import java.net.URLEncoder;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
@@ -188,11 +189,11 @@ public class UpdateShareRemoteOperation extends RemoteOperation {
        }

        if (note != null) {
            parametersToUpdate.add(new Pair<>(PARAM_NOTE, note));
            parametersToUpdate.add(new Pair<>(PARAM_NOTE, URLEncoder.encode(note)));
        }

        if (label != null) {
            parametersToUpdate.add(new Pair<>(PARAM_LABEL, label));
            parametersToUpdate.add(new Pair<>(PARAM_LABEL, URLEncoder.encode(label)));
        }

        /// perform required PUT requests