-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1604 from nextcloud/tos
ToS
- Loading branch information
Showing
8 changed files
with
198 additions
and
3 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
library/src/androidTest/java/com/nextcloud/android/lib/resources/tos/TermsOfServicesIT.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Nextcloud Android Library | ||
* | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-FileCopyrightText: 2024 Tobias Kaminsky <[email protected]> | ||
* 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 changes: 67 additions & 0 deletions
67
library/src/main/java/com/nextcloud/android/lib/resources/tos/GetTermsRemoteOperation.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Nextcloud Android Library | ||
* | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-FileCopyrightText: 2024 Tobias Kaminsky <[email protected]> | ||
* 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 changes: 46 additions & 0 deletions
46
library/src/main/java/com/nextcloud/android/lib/resources/tos/SignTermRemoteOperation.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Nextcloud Android Library | ||
* | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-FileCopyrightText: 2024 Tobias Kaminsky <[email protected]> | ||
* 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 changes: 17 additions & 0 deletions
17
library/src/main/java/com/nextcloud/android/lib/resources/tos/Term.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/* | ||
* Nextcloud Android Library | ||
* | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-FileCopyrightText: 2024 Tobias Kaminsky <[email protected]> | ||
* 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 changes: 15 additions & 0 deletions
15
library/src/main/java/com/nextcloud/android/lib/resources/tos/Terms.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/* | ||
* Nextcloud Android Library | ||
* | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-FileCopyrightText: 2024 Tobias Kaminsky <[email protected]> | ||
* 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> | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters