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

Unverified Commit 064d8c52 authored by Bartosz Przybylski's avatar Bartosz Przybylski Committed by tobiasKaminsky
Browse files

Add set user information operation

parent 68cf2ad9
Loading
Loading
Loading
Loading
+177 −0
Original line number Diff line number Diff line
/* Nextcloud Android Library is available under MIT license
 *
 *   @author Tobias Kaminsky
 *   Copyright (C) 2019 Tobias Kaminsky
 *   Copyright (C) 2019 Nextcloud GmbH
 *
 *   Permission is hereby granted, free of charge, to any person obtaining a copy
 *   of this software and associated documentation files (the "Software"), to deal
 *   in the Software without restriction, including without limitation the rights
 *   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *   copies of the Software, and to permit persons to whom the Software is
 *   furnished to do so, subject to the following conditions:
 *
 *   The above copyright notice and this permission notice shall be included in
 *   all copies or substantial portions of the Software.
 *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 *   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 *   BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 *   ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 *   CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *   THE SOFTWARE.
 *
 */

package com.owncloud.android.lib.resources.users;

import com.owncloud.android.AbstractIT;
import com.owncloud.android.lib.common.UserInfo;
import com.owncloud.android.lib.common.operations.RemoteOperationResult;

import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class SetRemoteUserInfoOperationTest extends AbstractIT {
    @Test
    public void testSetEmail() {
        RemoteOperationResult userInfo = new GetRemoteUserInfoOperation(userId).execute(client);
        assertTrue(userInfo.isSuccess());
        String oldValue = ((UserInfo) userInfo.getData().get(0)).email;

        // set
        assertTrue(new SetRemoteUserInfoOperation(userId,
                                                  SetRemoteUserInfoOperation.Field.EMAIL,
                                                  "new@mail.com")
                           .execute(client).isSuccess());

        userInfo = new GetRemoteUserInfoOperation(userId).execute(client);
        assertTrue(userInfo.isSuccess());
        assertEquals("new@mail.com", ((UserInfo) userInfo.getData().get(0)).email);

        // reset
        assertTrue(new SetRemoteUserInfoOperation(userId,
                                                  SetRemoteUserInfoOperation.Field.EMAIL,
                                                  oldValue)
                           .execute(client).isSuccess());
    }

    @Test
    public void testSetDisplayName() {
        RemoteOperationResult userInfo = new GetRemoteUserInfoOperation(userId).execute(client);
        assertTrue(userInfo.isSuccess());
        assertEquals(userId, ((UserInfo) userInfo.getData().get(0)).displayName);

        // set display name
        assertTrue(new SetRemoteUserInfoOperation(userId,
                                                  SetRemoteUserInfoOperation.Field.DISPLAYNAME,
                                                  "newName")
                           .execute(client).isSuccess());

        userInfo = new GetRemoteUserInfoOperation(userId).execute(client);
        assertTrue(userInfo.isSuccess());
        assertEquals("newName", ((UserInfo) userInfo.getData().get(0)).displayName);

        // reset
        assertTrue(new SetRemoteUserInfoOperation(userId,
                                                  SetRemoteUserInfoOperation.Field.DISPLAYNAME,
                                                  userId)
                           .execute(client).isSuccess());
    }

    @Test
    public void testSetPhone() {
        RemoteOperationResult userInfo = new GetRemoteUserInfoOperation(userId).execute(client);
        assertTrue(userInfo.isSuccess());
        String oldValue = ((UserInfo) userInfo.getData().get(0)).phone;

        // set
        assertTrue(new SetRemoteUserInfoOperation(userId,
                                                  SetRemoteUserInfoOperation.Field.PHONE,
                                                  "555-12345")
                           .execute(client).isSuccess());

        userInfo = new GetRemoteUserInfoOperation(userId).execute(client);
        assertTrue(userInfo.isSuccess());
        assertEquals("555-12345", ((UserInfo) userInfo.getData().get(0)).phone);

        // reset
        assertTrue(new SetRemoteUserInfoOperation(userId,
                                                  SetRemoteUserInfoOperation.Field.PHONE,
                                                  oldValue)
                           .execute(client).isSuccess());
    }

    @Test
    public void testSetAddress() {
        RemoteOperationResult userInfo = new GetRemoteUserInfoOperation(userId).execute(client);
        assertTrue(userInfo.isSuccess());
        String oldValue = ((UserInfo) userInfo.getData().get(0)).address;

        // set
        assertTrue(new SetRemoteUserInfoOperation(userId,
                                                  SetRemoteUserInfoOperation.Field.ADDRESS,
                                                  "NoName Street 123")
                           .execute(client).isSuccess());

        userInfo = new GetRemoteUserInfoOperation(userId).execute(client);
        assertTrue(userInfo.isSuccess());
        assertEquals("NoName Street 123", ((UserInfo) userInfo.getData().get(0)).address);

        // reset
        assertTrue(new SetRemoteUserInfoOperation(userId,
                                                  SetRemoteUserInfoOperation.Field.ADDRESS,
                                                  oldValue)
                           .execute(client).isSuccess());
    }

    @Test
    public void testSetWebsite() {
        RemoteOperationResult userInfo = new GetRemoteUserInfoOperation(userId).execute(client);
        assertTrue(userInfo.isSuccess());
        String oldValue = ((UserInfo) userInfo.getData().get(0)).website;

        // set
        assertTrue(new SetRemoteUserInfoOperation(userId,
                                                  SetRemoteUserInfoOperation.Field.WEBSITE,
                                                  "https://nextcloud.com")
                           .execute(client).isSuccess());

        userInfo = new GetRemoteUserInfoOperation(userId).execute(client);
        assertTrue(userInfo.isSuccess());
        assertEquals("https://nextcloud.com", ((UserInfo) userInfo.getData().get(0)).website);

        // reset
        assertTrue(new SetRemoteUserInfoOperation(userId,
                                                  SetRemoteUserInfoOperation.Field.WEBSITE,
                                                  oldValue)
                           .execute(client).isSuccess());
    }

    @Test
    public void testSetTwitter() {
        RemoteOperationResult userInfo = new GetRemoteUserInfoOperation(userId).execute(client);
        assertTrue(userInfo.isSuccess());
        String oldValue = ((UserInfo) userInfo.getData().get(0)).twitter;

        // set
        assertTrue(new SetRemoteUserInfoOperation(userId,
                                                  SetRemoteUserInfoOperation.Field.TWITTER,
                                                  "@Nextclouders")
                           .execute(client).isSuccess());

        userInfo = new GetRemoteUserInfoOperation(userId).execute(client);
        assertTrue(userInfo.isSuccess());
        assertEquals("@Nextclouders", ((UserInfo) userInfo.getData().get(0)).twitter);

        // reset
        assertTrue(new SetRemoteUserInfoOperation(userId,
                                                  SetRemoteUserInfoOperation.Field.TWITTER,
                                                  oldValue)
                           .execute(client).isSuccess());
    }
}
+106 −0
Original line number Diff line number Diff line
/*
 * Nextcloud Android client application
 *
 * @author Barotsz Przybylski
 * @author Tobias Kaminsky
 * Copyright (C) 2018 Bartosz Przybylski
 * Copyright (C) 2019 Tobias Kaminsky
 * Copyright (C) 2019 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 <http://www.gnu.org/licenses/>.
 */

package com.owncloud.android.lib.resources.users;

import com.owncloud.android.lib.common.OwnCloudClient;
import com.owncloud.android.lib.common.operations.RemoteOperation;
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
import com.owncloud.android.lib.common.utils.Log_OC;

import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PutMethod;


public class SetRemoteUserInfoOperation extends RemoteOperation {

    private static final String TAG = SetRemoteUserInfoOperation.class.getSimpleName();

    private static final String OCS_ROUTE_PATH = "/ocs/v1.php/cloud/users/";

    public enum Field {
        EMAIL("email"),
        DISPLAYNAME("displayname"),
        PHONE("phone"),
        ADDRESS("address"),
        WEBSITE("website"),
        TWITTER("twitter");

        private final String fieldName;

        Field(String fieldName) {
            this.fieldName = fieldName;
        }

        public String getFieldName() {
            return fieldName;
        }
    }

    private String mUserId;
    private Field mFieldName;
    private String mValue;

    public SetRemoteUserInfoOperation(String userId, Field fieldName, String value) {
        mUserId = userId;
        mFieldName = fieldName;
        mValue = value;
    }

    @Override
    protected RemoteOperationResult run(OwnCloudClient client) {
        RemoteOperationResult result;
        PutMethod method = null;

        try {
            method = new PutMethod(client.getBaseUri() + OCS_ROUTE_PATH + mUserId);
            method.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE);

            NameValuePair[] putParams = new NameValuePair[2];
            putParams[0] = new NameValuePair("key", mFieldName.getFieldName());
            putParams[1] = new NameValuePair("value", mValue);
            method.setQueryString(putParams);

            int status = client.executeMethod(method);

            if (status == HttpStatus.SC_OK) {
                result = new RemoteOperationResult(true, method);

            } else {
                result = new RemoteOperationResult(false, method);
                String response = method.getResponseBodyAsString();
                Log_OC.e(TAG, "Failed response while setting user information");
                Log_OC.e(TAG, "*** status code: " + status + "; response: " + response);
            }
        } catch (Exception e) {
            result = new RemoteOperationResult(e);
            Log_OC.e(TAG, "Exception while setting OC user information", e);
        } finally {
            if (method != null)
                method.releaseConnection();
        }

        return result;
    }
}