Skip to content

Commit

Permalink
Merge pull request #126 from nightly-labs/api-final-tests
Browse files Browse the repository at this point in the history
More api tests
  • Loading branch information
Giems authored Mar 13, 2024
2 parents 34d2c5d + c4c5e12 commit 40289c5
Show file tree
Hide file tree
Showing 22 changed files with 963 additions and 107 deletions.
3 changes: 3 additions & 0 deletions database/bindings/PrivilegeLevel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.

export type PrivilegeLevel = "Read" | "Edit" | "Admin";
111 changes: 57 additions & 54 deletions database/src/tables/user_app_privileges/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,43 +111,42 @@ impl Db {
DbError,
> {
let query = format!(
"WITH TeamJoinTimes AS (
SELECT
uap.team_id,
MAX(uap.creation_timestamp) as user_joined_team_timestamp
FROM
{USER_APP_PRIVILEGES_TABLE_NAME} uap
GROUP BY
uap.team_id
"WITH RelevantTeams AS (
SELECT DISTINCT t.team_id, t.team_name, t.personal, t.subscription,
t.registration_timestamp, gu.email AS team_admin_email,
gu.user_id AS team_admin_id,
CASE
WHEN t.team_admin_id = $1 THEN t.registration_timestamp
ELSE MAX(uap.creation_timestamp) OVER (PARTITION BY t.team_id)
END as user_joined_team_timestamp
FROM {TEAM_TABLE_NAME} t
LEFT JOIN {REGISTERED_APPS_TABLE_NAME} ra ON t.team_id = ra.team_id
LEFT JOIN {USER_APP_PRIVILEGES_TABLE_NAME} uap ON ra.app_id = uap.app_id AND uap.user_id = $1
JOIN {GRAFANA_USERS_TABLE_NAME} gu ON t.team_admin_id = gu.user_id
WHERE t.team_admin_id = $1 OR uap.user_id = $1
)
SELECT
t.team_id, t.team_name, t.personal, t.subscription, t.registration_timestamp, gu.email AS team_admin_email,
ra.app_id, ra.app_name, ra.whitelisted_domains, ra.ack_public_keys, ra.registration_timestamp, uap.user_id,
uap.privilege_level, uap.creation_timestamp, tjt.user_joined_team_timestamp
FROM
{TEAM_TABLE_NAME} t
JOIN
{REGISTERED_APPS_TABLE_NAME} ra ON t.team_id = ra.team_id
JOIN
{USER_APP_PRIVILEGES_TABLE_NAME} uap ON ra.app_id = uap.app_id
JOIN
{GRAFANA_USERS_TABLE_NAME} gu ON t.team_admin_id = gu.user_id
LEFT JOIN
TeamJoinTimes tjt ON t.team_id = tjt.team_id
WHERE
uap.user_id = $1
ORDER BY
t.team_id, ra.app_id"
SELECT rt.team_id, rt.team_name, rt.personal, rt.subscription, rt.registration_timestamp,
rt.team_admin_email, rt.team_admin_id, ra.app_id, ra.app_name, ra.whitelisted_domains,
ra.ack_public_keys, ra.registration_timestamp AS app_registration_timestamp,
uap.user_id, uap.privilege_level, uap.creation_timestamp AS privilege_creation_timestamp,
rt.user_joined_team_timestamp
FROM RelevantTeams rt
LEFT JOIN {REGISTERED_APPS_TABLE_NAME} ra ON rt.team_id = ra.team_id
LEFT JOIN {USER_APP_PRIVILEGES_TABLE_NAME} uap ON ra.app_id = uap.app_id AND uap.user_id = $1
ORDER BY rt.team_id, ra.app_id"
);

let rows = sqlx::query(&query)
.bind(user_id)
.fetch_all(&self.connection_pool)
.await
.map_err(|e| e.into());
.await;

if rows.is_err() {
return Err(rows.err().unwrap());
}
let rows = match rows {
Ok(rows) => rows,
Err(err) => {
return Err(err.into());
}
};

let mut team_app_map: HashMap<
String,
Expand All @@ -159,8 +158,7 @@ impl Db {
),
> = HashMap::new();

// Safe unwrap
for row in rows.unwrap() {
for row in rows {
let team = Team {
team_id: row.get("team_id"),
personal: row.get("personal"),
Expand All @@ -171,30 +169,35 @@ impl Db {
};

let admin_email = row.get("team_admin_email");

let app = DbRegisteredApp {
team_id: row.get("team_id"),
app_id: row.get("app_id"),
app_name: row.get("app_name"),
whitelisted_domains: row.get("whitelisted_domains"),
ack_public_keys: row.get("ack_public_keys"),
registration_timestamp: row.get("registration_timestamp"),
};

let privilege = UserAppPrivilege {
user_id: row.get("user_id"),
app_id: row.get("app_id"),
privilege_level: row.get("privilege_level"),
creation_timestamp: row.get("creation_timestamp"),
};

let user_joined_team_timestamp: DateTime<Utc> = row.get("user_joined_team_timestamp");

team_app_map
let team_id = team.team_id.clone();
let team_entry = team_app_map
.entry(team.team_id.clone())
.or_insert_with(|| (team, admin_email, user_joined_team_timestamp, Vec::new()))
.3
.push((app, privilege));
.or_insert_with(|| (team, admin_email, user_joined_team_timestamp, Vec::new()));

if let Ok(app_id) = row.try_get("app_id") {
if app_id != "" {
// Checking if app_id is present and not an empty string
let app = DbRegisteredApp {
team_id: team_id.clone(),
app_id,
app_name: row.get("app_name"),
whitelisted_domains: row.get("whitelisted_domains"),
ack_public_keys: row.get("ack_public_keys"),
registration_timestamp: row.get("app_registration_timestamp"),
};

let privilege = UserAppPrivilege {
user_id: row.get("user_id"),
app_id: app.app_id.clone(),
privilege_level: row.get("privilege_level"),
creation_timestamp: row.get("privilege_creation_timestamp"),
};

team_entry.3.push((app, privilege));
}
}
}

Ok(team_app_map.into_values().collect())
Expand Down
3 changes: 3 additions & 0 deletions database/src/tables/user_app_privileges/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ impl Db {
}
}

// Commit the transaction
tx.commit().await?;

Ok(())
}

Expand Down
3 changes: 3 additions & 0 deletions server/bindings/AppInfo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.

export interface AppInfo { teamId: string, appId: string, appName: string, registeredAt: string, whitelistedDomains: Array<string>, ackPublicKeys: Array<string>, }
2 changes: 1 addition & 1 deletion server/bindings/CloudApiErrors.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.

export type CloudApiErrors = "TeamDoesNotExist" | "UserDoesNotExist" | "CloudFeatureDisabled" | "InsufficientPermissions" | "TeamHasNoRegisteredApps" | "DatabaseError" | "MaximumUsersPerTeamReached" | "UserAlreadyBelongsToTheTeam" | "IncorrectPassword" | "AccessTokenFailure" | "RefreshTokenFailure" | "AppAlreadyExists" | "MaximumAppsPerTeamReached" | "TeamAlreadyExists" | "PersonalTeamAlreadyExists" | "EmailAlreadyExists" | "InternalServerError" | "UserDoesNotBelongsToTheTeam";
export type CloudApiErrors = "TeamDoesNotExist" | "UserDoesNotExist" | "CloudFeatureDisabled" | "InsufficientPermissions" | "TeamHasNoRegisteredApps" | "DatabaseError" | "MaximumUsersPerTeamReached" | "UserAlreadyBelongsToTheTeam" | "IncorrectPassword" | "AccessTokenFailure" | "RefreshTokenFailure" | "AppAlreadyExists" | "MaximumAppsPerTeamReached" | "TeamAlreadyExists" | "PersonalTeamAlreadyExists" | "EmailAlreadyExists" | "InternalServerError" | "UserDoesNotBelongsToTheTeam" | "InvalidName";
2 changes: 1 addition & 1 deletion server/bindings/HttpCloudEndpoint.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.

export type HttpCloudEndpoint = "/register_new_app" | "/register_with_password" | "/login_with_password" | "/register_new_team" | "/add_user_to_team" | "/remove_user_from_team";
export type HttpCloudEndpoint = "/register_new_app" | "/register_with_password" | "/login_with_password" | "/register_new_team" | "/add_user_to_team" | "/remove_user_from_team" | "/get_user_joined_teams";
6 changes: 6 additions & 0 deletions server/bindings/HttpGetUserJoinedTeamsResponse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
import type { AppInfo } from "./AppInfo";
import type { JoinedTeam } from "./JoinedTeam";
import type { UserPrivilege } from "./UserPrivilege";

export interface HttpGetUserJoinedTeamsResponse { teams: Record<string, JoinedTeam>, teams_apps: Record<string, Array<AppInfo>>, user_privileges: Record<string, Record<string, UserPrivilege>>, }
3 changes: 3 additions & 0 deletions server/bindings/JoinedTeam.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.

export interface JoinedTeam { teamId: string, teamName: string, creatorEmail: string, createdAt: string, joinedAt: string, personal: boolean, }
4 changes: 4 additions & 0 deletions server/bindings/UserPrivilege.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
import type { PrivilegeLevel } from "./PrivilegeLevel";

export interface UserPrivilege { appId: string, grantedAt: string, privilege: PrivilegeLevel, }
2 changes: 1 addition & 1 deletion server/src/bin/nightly-connect-server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use tracing_subscriber::EnvFilter;

#[tokio::main]
async fn main() {
let filter: EnvFilter = "debug,tower_http=trace,hyper=warn"
let filter: EnvFilter = "debug,sqlx=warn,tower_http=trace,hyper=warn"
.parse()
.expect("filter should parse");

Expand Down
Loading

0 comments on commit 40289c5

Please sign in to comment.