-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Admin::DomainAllow methods (#413)
* add methods for admin-related domain allowing
- Loading branch information
Showing
9 changed files
with
311 additions
and
11 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
bigbone-rx/src/main/kotlin/social/bigbone/rx/admin/RxAdminDomainAllowMethods.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,57 @@ | ||
package social.bigbone.rx.admin | ||
|
||
import io.reactivex.rxjava3.core.Completable | ||
import io.reactivex.rxjava3.core.Single | ||
import social.bigbone.MastodonClient | ||
import social.bigbone.api.Pageable | ||
import social.bigbone.api.Range | ||
import social.bigbone.api.entity.admin.AdminDomainAllow | ||
import social.bigbone.api.method.admin.AdminDomainAllowMethods | ||
|
||
/** | ||
* Reactive implementation of [AdminDomainAllowMethods]. | ||
* | ||
* Allow certain domains to federate. | ||
* @see <a href="https://docs.joinmastodon.org/methods/admin/domain_allows/">Mastodon admin/domain_allows API methods</a> | ||
*/ | ||
class RxAdminDomainAllowMethods(client: MastodonClient) { | ||
|
||
private val adminAllowMethods = AdminDomainAllowMethods(client) | ||
|
||
/** | ||
* Show information about all allowed domains. | ||
* @param range optional Range for the pageable return value | ||
* @see <a href="https://docs.joinmastodon.org/methods/domain_allows/#get">Mastodon API documentation: methods/domain_allows/#get</a> | ||
*/ | ||
@JvmOverloads | ||
fun getAllAllowedDomains(range: Range = Range()): Single<Pageable<AdminDomainAllow>> = Single.fromCallable { | ||
adminAllowMethods.getAllAllowedDomains(range = range).execute() | ||
} | ||
|
||
/** | ||
* Show information about a single allowed domain. | ||
* @param id The ID of the DomainAllow in the database. | ||
* @see <a href="https://docs.joinmastodon.org/methods/domain_allows/#get-one">Mastodon API documentation: methods/domain_allows/#get-one</a> | ||
*/ | ||
fun getAllowedDomain(id: String): Single<AdminDomainAllow> = Single.fromCallable { | ||
adminAllowMethods.getAllowedDomain(id = id).execute() | ||
} | ||
|
||
/** | ||
* Add a domain to the list of domains allowed to federate, to be used when the instance is in allow-list federation mode. | ||
* @param domain The domain to allow federation with. | ||
* @see <a href="https://docs.joinmastodon.org/methods/admin/domain_allows/#create">Mastodon API documentation: admin/domain_allows/#create</a> | ||
*/ | ||
fun allowDomainToFederate(domain: String): Single<AdminDomainAllow> = Single.fromCallable { | ||
adminAllowMethods.allowDomainToFederate(domain = domain).execute() | ||
} | ||
|
||
/** | ||
* Delete a domain from the allowed domains list. | ||
* @param id The ID of the DomainAllow in the database. | ||
* @see <a href="https://docs.joinmastodon.org/methods/admin/domain_allows/#delete">Mastodon API documentation: admin/domain_allows/#delete</a> | ||
*/ | ||
fun removeAllowedDomain(id: String): Completable = Completable.fromAction { | ||
adminAllowMethods.removeAllowedDomain(id = id) | ||
} | ||
} |
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
32 changes: 32 additions & 0 deletions
32
bigbone/src/main/kotlin/social/bigbone/api/entity/admin/AdminDomainAllow.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,32 @@ | ||
package social.bigbone.api.entity.admin | ||
|
||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
import social.bigbone.DateTimeSerializer | ||
import social.bigbone.PrecisionDateTime | ||
|
||
/** | ||
* Represents a domain allowed to federate. | ||
* @see <a href="https://docs.joinmastodon.org/entities/Admin_DomainAllow/">Mastodon API Admin::DomainAllow/</a> | ||
*/ | ||
@Serializable | ||
data class AdminDomainAllow( | ||
/** | ||
* The ID of the DomainAllow in the database. | ||
*/ | ||
@SerialName("id") | ||
val id: String = "0", | ||
|
||
/** | ||
* The domain that is allowed to federate. | ||
*/ | ||
@SerialName("domain") | ||
val domain: String = "", | ||
|
||
/** | ||
* When the domain was allowed to federate. | ||
*/ | ||
@SerialName("created_at") | ||
@Serializable(with = DateTimeSerializer::class) | ||
val createdAt: PrecisionDateTime = PrecisionDateTime.InvalidPrecisionDateTime.Unavailable | ||
) |
70 changes: 70 additions & 0 deletions
70
bigbone/src/main/kotlin/social/bigbone/api/method/admin/AdminDomainAllowMethods.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,70 @@ | ||
package social.bigbone.api.method.admin | ||
|
||
import social.bigbone.MastodonClient | ||
import social.bigbone.MastodonRequest | ||
import social.bigbone.Parameters | ||
import social.bigbone.api.Pageable | ||
import social.bigbone.api.Range | ||
import social.bigbone.api.entity.admin.AdminDomainAllow | ||
|
||
/** | ||
* Allow certain domains to federate. | ||
* @see <a href="https://docs.joinmastodon.org/methods/admin/domain_allows/">Mastodon admin/domain_allows API methods</a> | ||
*/ | ||
class AdminDomainAllowMethods(private val client: MastodonClient) { | ||
|
||
private val adminDomainAllowEndpoint = "api/v1/admin/domain_allows" | ||
|
||
/** | ||
* Show information about all allowed domains. | ||
* @param range optional Range for the pageable return value | ||
* @see <a href="https://docs.joinmastodon.org/methods/domain_allows/#get">Mastodon API documentation: methods/domain_allows/#get</a> | ||
*/ | ||
@JvmOverloads | ||
fun getAllAllowedDomains(range: Range = Range()): MastodonRequest<Pageable<AdminDomainAllow>> { | ||
return client.getPageableMastodonRequest( | ||
endpoint = adminDomainAllowEndpoint, | ||
method = MastodonClient.Method.GET, | ||
parameters = range.toParameters() | ||
) | ||
} | ||
|
||
/** | ||
* Show information about a single allowed domain. | ||
* @param id The ID of the DomainAllow in the database. | ||
* @see <a href="https://docs.joinmastodon.org/methods/domain_allows/#get-one">Mastodon API documentation: methods/domain_allows/#get-one</a> | ||
*/ | ||
fun getAllowedDomain(id: String): MastodonRequest<AdminDomainAllow> { | ||
return client.getMastodonRequest( | ||
endpoint = "$adminDomainAllowEndpoint/$id", | ||
method = MastodonClient.Method.GET | ||
) | ||
} | ||
|
||
/** | ||
* Add a domain to the list of domains allowed to federate, to be used when the instance is in allow-list federation mode. | ||
* @param domain The domain to allow federation with. | ||
* @see <a href="https://docs.joinmastodon.org/methods/admin/domain_allows/#create">Mastodon API documentation: admin/domain_allows/#create</a> | ||
*/ | ||
fun allowDomainToFederate(domain: String): MastodonRequest<AdminDomainAllow> { | ||
return client.getMastodonRequest( | ||
endpoint = adminDomainAllowEndpoint, | ||
method = MastodonClient.Method.POST, | ||
parameters = Parameters().apply { | ||
append("domain", domain) | ||
} | ||
) | ||
} | ||
|
||
/** | ||
* Delete a domain from the allowed domains list. | ||
* @param id The ID of the DomainAllow in the database. | ||
* @see <a href="https://docs.joinmastodon.org/methods/admin/domain_allows/#delete">Mastodon API documentation: admin/domain_allows/#delete</a> | ||
*/ | ||
fun removeAllowedDomain(id: String) { | ||
client.performAction( | ||
endpoint = "$adminDomainAllowEndpoint/$id", | ||
method = MastodonClient.Method.DELETE | ||
) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"id": "1", | ||
"domain": "mastodon.social", | ||
"created_at": "2022-09-14T21:23:02.755Z" | ||
} |
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,12 @@ | ||
[ | ||
{ | ||
"id": "2", | ||
"domain": "mastodon", | ||
"created_at": "2022-09-14T21:24:15.360Z" | ||
}, | ||
{ | ||
"id": "1", | ||
"domain": "mastodon.social", | ||
"created_at": "2022-09-14T21:23:02.755Z" | ||
} | ||
] |
116 changes: 116 additions & 0 deletions
116
bigbone/src/test/kotlin/social/bigbone/api/method/admin/AdminDomainAllowMethodsTest.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,116 @@ | ||
package social.bigbone.api.method.admin | ||
|
||
import io.mockk.slot | ||
import io.mockk.verify | ||
import org.amshove.kluent.AnyException | ||
import org.amshove.kluent.invoking | ||
import org.amshove.kluent.shouldBeEqualTo | ||
import org.amshove.kluent.shouldNotThrow | ||
import org.amshove.kluent.shouldThrow | ||
import org.amshove.kluent.withMessage | ||
import org.junit.jupiter.api.Test | ||
import social.bigbone.MastodonClient | ||
import social.bigbone.Parameters | ||
import social.bigbone.api.Range | ||
import social.bigbone.api.exception.BigBoneRequestException | ||
import social.bigbone.testtool.MockClient | ||
|
||
class AdminDomainAllowMethodsTest { | ||
|
||
private val adminDomainAllowEndpoint = "api/v1/admin/domain_allows" | ||
|
||
@Test | ||
fun `Given client returning success, when getting all allowed domains, then return list of allowed domains`() { | ||
val client = MockClient.mock("admin_domain_allow_list.json") | ||
val adminDomainAllowMethods = AdminDomainAllowMethods(client) | ||
val range = Range("1", "10", "1", 10) | ||
val allowedDomains = adminDomainAllowMethods.getAllAllowedDomains(range).execute() | ||
allowedDomains.part.size shouldBeEqualTo 2 | ||
with(allowedDomains.part[1]) { | ||
id shouldBeEqualTo "1" | ||
domain shouldBeEqualTo "mastodon.social" | ||
} | ||
|
||
val parametersCapturingSlot = slot<Parameters>() | ||
verify { | ||
client.get( | ||
path = adminDomainAllowEndpoint, | ||
query = capture(parametersCapturingSlot) | ||
) | ||
} | ||
with(parametersCapturingSlot.captured) { | ||
toQuery() shouldBeEqualTo "max_id=${range.maxId}&min_id=${range.minId}&since_id=${range.sinceId}&limit=${range.limit}" | ||
} | ||
} | ||
|
||
@Test | ||
fun `Given client returning success, when getting specific allowed domain, then return expected data for single domain`() { | ||
val client = MockClient.mock("admin_domain_allow.json") | ||
val adminDomainAllowMethods = AdminDomainAllowMethods(client) | ||
val pathVariable = "1" | ||
val adminDomainAllowed = adminDomainAllowMethods.getAllowedDomain(pathVariable).execute() | ||
with(adminDomainAllowed) { | ||
id shouldBeEqualTo pathVariable | ||
domain shouldBeEqualTo "mastodon.social" | ||
} | ||
|
||
verify { | ||
client.get( | ||
path = "$adminDomainAllowEndpoint/$pathVariable" | ||
) | ||
} | ||
} | ||
|
||
@Test | ||
fun `Given client returning success, when posting a domain to be allowed to federate, then return expected data about the domain`() { | ||
val client = MockClient.mock("admin_domain_allow.json") | ||
val adminDomainAllowMethods = AdminDomainAllowMethods(client) | ||
|
||
invoking { | ||
adminDomainAllowMethods.allowDomainToFederate(domain = "mastodon.social").execute() | ||
} shouldNotThrow AnyException | ||
|
||
val parametersCapturingSlot = slot<Parameters>() | ||
verify { | ||
client.post( | ||
path = adminDomainAllowEndpoint, | ||
body = capture(parametersCapturingSlot), | ||
addIdempotencyKey = false | ||
) | ||
} | ||
with(parametersCapturingSlot.captured) { | ||
toQuery() shouldBeEqualTo "domain=mastodon.social" | ||
} | ||
} | ||
|
||
@Test | ||
fun `Given client returning success, when removing specific domain from being allowd, then check data for single ip`() { | ||
val client = MockClient.mock("admin_domain_allow.json") | ||
val adminDomainAllowMethods = AdminDomainAllowMethods(client) | ||
val id = "1" | ||
|
||
invoking { | ||
adminDomainAllowMethods.removeAllowedDomain(id = id) | ||
} shouldNotThrow AnyException | ||
|
||
verify { | ||
client.performAction( | ||
endpoint = "$adminDomainAllowEndpoint/$id", | ||
method = MastodonClient.Method.DELETE | ||
) | ||
} | ||
} | ||
|
||
@Test | ||
fun `Given a client returning forbidden, when getting specific allowed domain, then propagate error`() { | ||
val client = MockClient.failWithResponse( | ||
responseJsonAssetPath = "error_403_forbidden.json", | ||
responseCode = 403, | ||
message = "Forbidden" | ||
) | ||
|
||
invoking { | ||
AdminDomainAllowMethods(client).getAllowedDomain(id = "1").execute() | ||
} shouldThrow BigBoneRequestException::class withMessage "Forbidden" | ||
} | ||
} |
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