Skip to content

Commit

Permalink
feat: matrix-admin and e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
EstebanBorai committed Nov 19, 2023
1 parent 4c8cea2 commit 23071fb
Show file tree
Hide file tree
Showing 27 changed files with 784 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# Commune
#
# The shared secret is used to authenticate the registration requests.
#
# This is explicitly passed here for development purposes, it should match the
# same as on `fixtures/synapse/homeserver.yaml` for CI.
COMMUNE_REGISTRATION_SHARED_SECRET='m@;wYOUOh0f:CH5XA65sJB1^q01~DmIriOysRImot,OR_vzN&B'

# Matrix Client
MATRIX_HOST=http://localhost:8008
MATRIX_ADMIN_TOKEN=secret
Expand Down
27 changes: 27 additions & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!--
Developer's Certificate of Origin 1.1
By making a contribution to this project, I certify that:
(a) The contribution was created in whole or in part by me and I
have the right to submit it under the open source license
indicated in the file; or
(b) The contribution is based upon previous work that, to the best
of my knowledge, is covered under an appropriate open source
license and I have the right under that license to submit that
work with modifications, whether created in whole or in part
by me, under the same open source license (unless I am
permitted to submit under a different license), as indicated
in the file; or
(c) The contribution was provided directly to me by some other
person who certified (a), (b) or (c) and I have not modified
it.
(d) I understand and agree that this project and the contribution
are public and that a record of the contribution (including all
personal information I submit with it, including my sign-off) is
maintained indefinitely and may be redistributed consistent with
this project or the open source license(s) involved.
-->
9 changes: 9 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Please see the documentation for all configuration options:
# https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates

version: 2
updates:
- package-ecosystem: 'cargo'
directory: '/'
schedule:
interval: 'weekly'
53 changes: 53 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: Continuous Integration

on:
push:
branches:
- main
pull_request:
branches: [main]
paths:
- "**"
- "!/*.md"
- "!/**.md"

concurrency:
group: "${{ github.workflow }} @ ${{ github.event.pull_request.head.label || github.head_ref || github.ref }}"
cancel-in-progress: true

jobs:
ci:
name: CI
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup Rust Toolchain
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable

- name: Setup Rust Cache
uses: Swatinem/rust-cache@v2

- name: Check formatting
run: cargo fmt --check

- name: Check clippy
run: cargo clippy --workspace -- -D warnings

- name: Unit Tests
run: cargo test -p matrix

- name: Install Just
uses: extractions/setup-just@v1

- name: Prepare Data for Tests
run: |
cp -R ./crates/test/fixtures/synapse ./docker/synapse
docker compose up -d
- name: E2E Tests
run: cargo test -p test -- --test-threads=1
21 changes: 21 additions & 0 deletions .github/workflows/dependabot-auto-approve.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Dependabot auto-approve
on: pull_request_target

permissions:
pull-requests: write

jobs:
dependabot:
runs-on: ubuntu-latest
if: ${{ github.actor == 'dependabot[bot]' }}
steps:
- name: Dependabot metadata
id: metadata
uses: dependabot/[email protected]
with:
github-token: "${{ secrets.GITHUB_TOKEN }}"
- name: Approve a PR
run: gh pr review --approve "$PR_URL"
env:
PR_URL: ${{github.event.pull_request.html_url}}
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
23 changes: 23 additions & 0 deletions .github/workflows/dependabot-auto-merge.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Dependabot auto-merge
on: pull_request_target

permissions:
pull-requests: write
contents: write

jobs:
dependabot:
runs-on: ubuntu-latest
if: ${{ github.actor == 'dependabot[bot]' }}
steps:
- name: Dependabot metadata
id: metadata
uses: dependabot/[email protected]
with:
github-token: "${{ secrets.GITHUB_TOKEN }}"
- name: Enable auto-merge for Dependabot PRs
if: ${{steps.metadata.outputs.update-type == 'version-update:semver-patch'}}
run: gh pr merge --auto --squash "$PR_URL"
env:
PR_URL: ${{github.event.pull_request.html_url}}
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
10 changes: 10 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[workspace]
members = [
"crates/matrix",
"crates/test"
]
resolver = "1"

[workspace.dependencies]
serde = "1.0.192"
url = { version = "2.4.1", features = ["serde"] }
4 changes: 4 additions & 0 deletions Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,7 @@ stop:
clear: stop
docker compose rm --all --force --volumes --stop
docker volume rm commune_synapse_database || true

# Runs all the tests from the `test` package. Optionally runs a single one if name pattern is provided
e2e *args='':
cargo test --package test -- --test-threads=1 $1
20 changes: 20 additions & 0 deletions crates/matrix/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "matrix"
version = "0.1.0"
edition = "2021"
publish = false

[dependencies]
anyhow = "1.0.75"
async-trait = "0.1.74"
hex = "0.4.3"
hmac = "0.12.1"
matrix-sdk = { git = "https://github.com/matrix-org/matrix-rust-sdk.git", rev = "e43a25a" }
reqwest = { version = "0.11.22", features = ["json"] }
serde_path_to_error = "0.1.14"
serde_qs = "0.12.0"
sha1 = "0.10.6"

# Workspace Dependencies
serde = { workspace = true }
url = { workspace = true }
137 changes: 137 additions & 0 deletions crates/matrix/src/admin/client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
use std::ops::Deref;
use std::str::from_utf8;

use anyhow::{bail, Result};
use reqwest::header::{HeaderMap, HeaderValue, AUTHORIZATION};
use reqwest::{Client as HttpClient, Response};
use serde::Serialize;
use url::Url;

pub struct Client {
client: HttpClient,
base_url: Url,
token: Option<String>,
}

impl Client {
pub fn new<S: AsRef<str>>(url: S) -> Result<Self> {
let url = Url::parse(url.as_ref())?;

Ok(Self {
client: HttpClient::new(),
base_url: url,
token: None,
})
}

/// Sets the token to be used for authentication with the server.
pub fn set_token(&mut self, token: impl Into<String>) -> Result<()> {
let token = token.into();

if token.is_empty() {
self.token = None;
bail!("Token cannot be empty");
}

self.token = Some(token);
Ok(())
}

pub async fn get(&self, path: impl AsRef<str>) -> Result<Response> {
let url = self.build_url(path)?;
let headers = self.build_headers()?;
let response = self.client.get(url).headers(headers).send().await?;

Ok(response)
}

pub async fn get_query(
&self,
path: impl AsRef<str>,
params: impl Serialize,
) -> Result<Response> {
let url = self.build_url_with_params(path, params)?;
let headers = self.build_headers()?;
let response = self.client.get(url).headers(headers).send().await?;

Ok(response)
}

pub async fn post_json<T>(&self, path: impl AsRef<str>, body: &T) -> Result<Response>
where
T: Serialize,
{
let url = self.build_url(path)?;
let headers = self.build_headers()?;
let resp = self
.client
.post(url)
.json(body)
.headers(headers)
.send()
.await?;

Ok(resp)
}

pub async fn put_json<T>(&self, path: impl AsRef<str>, body: &T) -> Result<Response>
where
T: Serialize,
{
let url = self.build_url(path)?;
let headers = self.build_headers()?;
let resp = self
.client
.put(url)
.json(body)
.headers(headers)
.send()
.await?;

Ok(resp)
}

fn build_headers(&self) -> Result<HeaderMap> {
let mut headers = HeaderMap::new();

if let Some(token) = &self.token {
headers.insert(
AUTHORIZATION,
HeaderValue::from_str(&format!("Bearer {}", token))?,
);
}

Ok(headers)
}

#[inline]
fn build_url(&self, path: impl AsRef<str>) -> Result<Url> {
let mut next = self.base_url.clone();

next.set_path(path.as_ref());

Ok(next)
}

fn build_url_with_params(&self, path: impl AsRef<str>, params: impl Serialize) -> Result<Url> {
let mut url = self.build_url(path)?;
let mut buff = Vec::new();
let qs_ser = &mut serde_qs::Serializer::new(&mut buff);

serde_path_to_error::serialize(&params, qs_ser)?;

let params = from_utf8(buff.as_slice())?.to_string();

url.set_query(Some(&params));

Ok(url)
}
}

impl Deref for Client {
type Target = HttpClient;

fn deref(&self) -> &Self::Target {
&self.client
}
}
4 changes: 4 additions & 0 deletions crates/matrix/src/admin/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub mod client;
pub mod resources;

pub use client::Client;
1 change: 1 addition & 0 deletions crates/matrix/src/admin/resources/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod token;
1 change: 1 addition & 0 deletions crates/matrix/src/admin/resources/token/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod shared_secret;
Loading

0 comments on commit 23071fb

Please sign in to comment.