Skip to content

Commit

Permalink
Try to fix seed in test env
Browse files Browse the repository at this point in the history
  • Loading branch information
Heliozoa committed Jul 11, 2023
1 parent df8bd2b commit f6f3bc6
Show file tree
Hide file tree
Showing 13 changed files with 180 additions and 36 deletions.
2 changes: 1 addition & 1 deletion kubernetes/test/headless-lms/env.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ data:
JWT_PASSWORD: "PzMXyEkzKFRN83q8DKAWZ0laQkXwPlUDh4hUwBJdO4c="
OAUTH_APPLICATION_ID: "TkY0cFNhWThVWVk0MjViOWdYekpWaGlMdV9RMVpMMnNGdEZSMS1nZ3hnMA=="
OAUTH_SECRET: "LVJlaTVuVzJsUlVwbjRGTnJQallLellJVTBiYlpHQy1PX2JIcmlZb0NYaw=="
BASE_URL: "aHR0cDovL3Byb2plY3QtMzMxLmxvY2Fs"
BASE_URL: "aHR0cHM6Ly9jb3Vyc2VzLm1vb2MuZmk="
TEST_MODE: "dHJ1ZQ=="
RUST_BACKTRACE: "MQ=="
ALLOW_NO_HTTPS_FOR_DEVELOPMENT: "dHJ1ZQ=="
Expand Down
9 changes: 7 additions & 2 deletions services/headless-lms/server/src/controllers/cms/pages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,14 @@ Content-Type: application/json
```
*/
#[generated_doc]
#[instrument(skip(pool))]
#[instrument(skip(pool, app_conf))]
async fn update_page(
request_id: RequestId,
payload: web::Json<CmsPageUpdate>,
page_id: web::Path<Uuid>,
pool: web::Data<PgPool>,
jwt_key: web::Data<JwtKey>,
app_conf: web::Data<ApplicationConfiguration>,
user: AuthUser,
) -> ControllerResult<web::Json<ContentManagementPage>> {
let mut conn = pool.acquire().await?;
Expand All @@ -102,7 +103,11 @@ async fn update_page(
history_change_reason: HistoryChangeReason::PageSaved,
is_exam_page,
},
models_requests::make_spec_fetcher(request_id.0, jwt_key.into_inner()),
models_requests::make_spec_fetcher(
app_conf.base_url.clone(),
request_id.0,
jwt_key.into_inner(),
),
models_requests::fetch_service_info,
)
.await?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ async fn post_new_chapter(
PKeyPolicy::Generate,
&new_chapter,
user.id,
models_requests::make_spec_fetcher(request_id.0, Arc::clone(&jwt_key)),
models_requests::make_spec_fetcher(
app_conf.base_url.clone(),
request_id.0,
Arc::clone(&jwt_key),
),
models_requests::fetch_service_info,
)
.await?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,13 @@ Content-Type: application/json
```
*/
#[generated_doc]
#[instrument(skip(pool))]
#[instrument(skip(pool, app_conf))]
async fn post_new_course(
request_id: RequestId,
pool: web::Data<PgPool>,
payload: web::Json<NewCourse>,
user: AuthUser,
app_conf: web::Data<ApplicationConfiguration>,
jwt_key: web::Data<JwtKey>,
) -> ControllerResult<web::Json<Course>> {
let mut conn = pool.acquire().await?;
Expand All @@ -121,7 +122,11 @@ async fn post_new_course(
PKeyPolicy::Generate,
new_course,
user.id,
models_requests::make_spec_fetcher(request_id.0, Arc::clone(&jwt_key)),
models_requests::make_spec_fetcher(
app_conf.base_url.clone(),
request_id.0,
Arc::clone(&jwt_key),
),
models_requests::fetch_service_info,
)
.await?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@ Content-Type: application/json
```
*/
#[generated_doc]
#[instrument(skip(pool))]
#[instrument(skip(pool, app_conf))]
async fn post_new_page(
request_id: RequestId,
payload: web::Json<NewPage>,
pool: web::Data<PgPool>,
app_conf: web::Data<ApplicationConfiguration>,
user: AuthUser,
jwt_key: web::Data<JwtKey>,
) -> ControllerResult<web::Json<Page>> {
Expand All @@ -67,7 +68,11 @@ async fn post_new_page(
&mut conn,
new_page,
user.id,
models_requests::make_spec_fetcher(request_id.0, Arc::clone(&jwt_key)),
models_requests::make_spec_fetcher(
app_conf.base_url.clone(),
request_id.0,
Arc::clone(&jwt_key),
),
models_requests::fetch_service_info,
)
.await?;
Expand Down Expand Up @@ -136,12 +141,13 @@ async fn history_count(
POST /api/v0/main-frontend/pages/:page_id/restore
*/
#[generated_doc]
#[instrument(skip(pool))]
#[instrument(skip(pool, app_conf))]
async fn restore(
request_id: RequestId,
pool: web::Data<PgPool>,
page_id: web::Path<Uuid>,
restore_data: web::Json<HistoryRestoreData>,
app_conf: web::Data<ApplicationConfiguration>,
user: AuthUser,
jwt_key: web::Data<JwtKey>,
) -> ControllerResult<web::Json<Uuid>> {
Expand All @@ -152,7 +158,11 @@ async fn restore(
*page_id,
restore_data.history_id,
user.id,
models_requests::make_spec_fetcher(request_id.0, Arc::clone(&jwt_key)),
models_requests::make_spec_fetcher(
app_conf.base_url.clone(),
request_id.0,
Arc::clone(&jwt_key),
),
models_requests::fetch_service_info,
)
.await?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,11 @@ pub async fn get_edit_proposal_count(
/**
POST `/api/v0/main-frontend/proposed-edits/process-edit-proposal` - Processes the given edit proposal.
*/
#[instrument(skip(pool))]
#[instrument(skip(pool, app_conf))]
pub async fn process_edit_proposal(
request_id: RequestId,
proposal: web::Json<EditProposalInfo>,
app_conf: web::Data<ApplicationConfiguration>,
user: AuthUser,
pool: web::Data<PgPool>,
jwt_key: web::Data<JwtKey>,
Expand All @@ -94,7 +95,11 @@ pub async fn process_edit_proposal(
proposal.page_proposal_id,
proposal.block_proposals,
user.id,
models_requests::make_spec_fetcher(request_id.0, Arc::clone(&jwt_key)),
models_requests::make_spec_fetcher(
app_conf.base_url.clone(),
request_id.0,
Arc::clone(&jwt_key),
),
models_requests::fetch_service_info,
)
.await?;
Expand Down
11 changes: 6 additions & 5 deletions services/headless-lms/server/src/domain/models_requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,14 +233,15 @@ pub struct SpecRequest<'a> {
/// Fetches a public/model spec based on the private spec from the given url.
/// The slug and jwt key are used for an upload claim that allows the service
/// to upload files as part of the spec.
pub fn make_spec_fetcher(request_id: Uuid, jwt_key: Arc<JwtKey>) -> impl SpecFetcher {
pub fn make_spec_fetcher(
base_url: String,
request_id: Uuid,
jwt_key: Arc<JwtKey>,
) -> impl SpecFetcher {
move |url, exercise_service_slug, private_spec| {
let client = reqwest::Client::new();
let upload_claim = UploadClaim::expiring_in_1_day(exercise_service_slug.into());
// TODO: use real url
let upload_url = Some(format!(
"http://project-331.local/api/v0/files/{exercise_service_slug}"
));
let upload_url = Some(format!("{base_url}/api/v0/files/{exercise_service_slug}"));
let req = client
.post(url.clone())
.header(
Expand Down
4 changes: 4 additions & 0 deletions services/headless-lms/server/src/programs/seed/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ use std::{env, process::Command, sync::Arc};

use crate::{domain::models_requests::JwtKey, setup_tracing};

use anyhow::Context;
use futures::try_join;

use headless_lms_utils::futures::run_parallelly;
use sqlx::{migrate::MigrateDatabase, postgres::PgPoolOptions, Pool, Postgres};
use tracing::info;

pub async fn main() -> anyhow::Result<()> {
let base_url = std::env::var("BASE_URL").context("BASE_URL must be defined")?;
let db_pool = setup_seed_environment().await?;
let jwt_key = Arc::new(JwtKey::try_from_env().expect("Failed to create JwtKey"));

Expand All @@ -41,12 +43,14 @@ pub async fn main() -> anyhow::Result<()> {
run_parallelly(seed_organizations::uh_cs::seed_organization_uh_cs(
db_pool.clone(),
seed_users_result.clone(),
base_url.clone(),
Arc::clone(&jwt_key),
)),
run_parallelly(
seed_organizations::uh_mathstat::seed_organization_uh_mathstat(
db_pool.clone(),
seed_users_result.clone(),
base_url.clone(),
Arc::clone(&jwt_key),
)
)
Expand Down
Loading

0 comments on commit f6f3bc6

Please sign in to comment.