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

Unverified Commit fe8b18f5 authored by Tobias Kaminsky's avatar Tobias Kaminsky Committed by GitHub
Browse files

Merge pull request #1604 from nextcloud/tos

ToS
parents e3c06119 f4f71d5e
Loading
Loading
Loading
Loading
+39 −0
Original line number Diff line number Diff line
/*
 * Nextcloud Android Library
 *
 * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
 * SPDX-FileCopyrightText: 2024 Tobias Kaminsky <tobias@kaminsky.me>
 * SPDX-License-Identifier: MIT
 */

package com.nextcloud.android.lib.resources.tos

import com.owncloud.android.AbstractIT
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue

class TermsOfServicesIT : AbstractIT() {
    // @Test disabled for now as no good way to test on CI
    fun getAndSignTerms() {
        // user 3 with ToS
        var result = GetTermsRemoteOperation().execute(nextcloudClient)
        assertTrue(result.isSuccess)

        var terms = result.resultData
        assertTrue(terms.terms.isNotEmpty())
        assertFalse(terms.hasSigned)

        val id = terms.terms[0].id

        // sign
        assertTrue(SignTermRemoteOperation(id).execute(nextcloudClient).isSuccess)

        // signed terms
        result = GetTermsRemoteOperation().execute(nextcloudClient)
        assertTrue(result.isSuccess)

        terms = result.resultData
        assertTrue(terms.terms.isNotEmpty())
        assertTrue(terms.hasSigned)
    }
}
+67 −0
Original line number Diff line number Diff line
/*
 * Nextcloud Android Library
 *
 * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
 * SPDX-FileCopyrightText: 2024 Tobias Kaminsky <tobias@kaminsky.me>
 * SPDX-License-Identifier: MIT
 */
package com.nextcloud.android.lib.resources.tos

import com.google.gson.reflect.TypeToken
import com.nextcloud.common.NextcloudClient
import com.nextcloud.operations.GetMethod
import com.owncloud.android.lib.common.operations.RemoteOperationResult
import com.owncloud.android.lib.common.utils.Log_OC
import com.owncloud.android.lib.ocs.ServerResponse
import com.owncloud.android.lib.resources.OCSRemoteOperation
import org.apache.commons.httpclient.HttpStatus

/**
 * Get terms of service of an user
 */
class GetTermsRemoteOperation : OCSRemoteOperation<Terms>() {
    @Suppress("TooGenericExceptionCaught")
    override fun run(client: NextcloudClient): RemoteOperationResult<Terms> {
        var result: RemoteOperationResult<Terms>
        var getMethod: GetMethod? = null
        try {
            getMethod =
                GetMethod(
                    client.baseUri.toString() + ENDPOINT + JSON_FORMAT,
                    true
                )
            val status = client.execute(getMethod)
            if (status == HttpStatus.SC_OK) {
                val terms =
                    getServerResponse(
                        getMethod,
                        object : TypeToken<ServerResponse<Terms>>() {}
                    )?.ocs?.data

                if (terms != null) {
                    result = RemoteOperationResult(true, getMethod)
                    result.setResultData(terms)
                } else {
                    result = RemoteOperationResult(false, getMethod)
                }
            } else {
                result = RemoteOperationResult(false, getMethod)
            }
        } catch (e: Exception) {
            result = RemoteOperationResult(e)
            Log_OC.e(
                TAG,
                "Get terms failed: " + result.logMessage,
                result.exception
            )
        } finally {
            getMethod?.releaseConnection()
        }
        return result
    }

    companion object {
        private val TAG = GetTermsRemoteOperation::class.java.simpleName
        private const val ENDPOINT = "/ocs/v2.php/apps/terms_of_service/terms"
    }
}
+46 −0
Original line number Diff line number Diff line
/*
 * Nextcloud Android Library
 *
 * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
 * SPDX-FileCopyrightText: 2024 Tobias Kaminsky <tobias@kaminsky.me>
 * SPDX-License-Identifier: MIT
 */
package com.nextcloud.android.lib.resources.tos

import com.nextcloud.common.NextcloudClient
import com.nextcloud.operations.PostMethod
import com.owncloud.android.lib.common.operations.RemoteOperation
import com.owncloud.android.lib.common.operations.RemoteOperationResult
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody.Companion.toRequestBody
import org.apache.commons.httpclient.HttpStatus

/**
 * Sign terms of services
 */
class SignTermRemoteOperation(
    val id: Int
) : RemoteOperation<Void>() {
    @Suppress("TooGenericExceptionCaught")
    override fun run(client: NextcloudClient): RemoteOperationResult<Void> {
        val requestBody = hashMapOf("termId" to id)

        val json = gson.toJson(requestBody)

        val request = json.toRequestBody("application/json".toMediaTypeOrNull())

        val postMethod = PostMethod(client.baseUri.toString() + ENDPOINT, true, request)

        val status = postMethod.execute(client)

        return if (status == HttpStatus.SC_OK) {
            RemoteOperationResult<Void>(true, postMethod)
        } else {
            RemoteOperationResult<Void>(false, postMethod)
        }
    }

    companion object {
        private const val ENDPOINT = "/ocs/v2.php/apps/terms_of_service/sign"
    }
}
+17 −0
Original line number Diff line number Diff line
/*
 * Nextcloud Android Library
 *
 * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
 * SPDX-FileCopyrightText: 2024 Tobias Kaminsky <tobias@kaminsky.me>
 * SPDX-License-Identifier: MIT
 */

package com.nextcloud.android.lib.resources.tos

data class Term(
    val id: Int,
    val countryCode: String,
    val languageCode: String,
    val body: String,
    val renderedBody: String
)
+15 −0
Original line number Diff line number Diff line
/*
 * Nextcloud Android Library
 *
 * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
 * SPDX-FileCopyrightText: 2024 Tobias Kaminsky <tobias@kaminsky.me>
 * SPDX-License-Identifier: MIT
 */

package com.nextcloud.android.lib.resources.tos

data class Terms(
    val terms: List<Term>,
    val hasSigned: Boolean,
    val languages: Map<String, String>
)
Loading