Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions contracts/course-registry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ pub struct CourseStatusChanged {
pub active: bool,
}

#[contractevent]
pub struct OwnershipTransferred {
#[topic]
pub course_id: u32,
#[topic]
pub previous_instructor: Address,
pub new_instructor: Address,
}

#[contractevent]
pub struct ModuleCompleted {
#[topic]
Expand Down Expand Up @@ -234,6 +243,40 @@ impl CourseRegistry {
env.storage().persistent().get(&key).unwrap_or(0)
}

/// Transfers ownership of a course to a new instructor address.
/// Only callable by the current instructor of the course.
pub fn transfer_ownership(
env: Env,
current_instructor: Address,
new_instructor: Address,
course_id: u32,
) {
let mut course: Course = env
.storage()
.persistent()
.get(&DataKey::Course(course_id))
.expect("Course not found");

assert!(
course.instructor == current_instructor,
"Unauthorized: Caller is not the course instructor"
);

current_instructor.require_auth();

course.instructor = new_instructor.clone();
env.storage()
.persistent()
.set(&DataKey::Course(course_id), &course);

OwnershipTransferred {
course_id,
previous_instructor: current_instructor,
new_instructor,
}
.publish(&env);
}

/// Records a learner's completion of a module after off-chain quiz validation.
/// Only callable by the authorized verifier (protocol admin).
pub fn complete_module(env: Env, verifier: Address, learner: Address, id: u32) {
Expand Down
84 changes: 84 additions & 0 deletions contracts/course-registry/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -563,3 +563,87 @@ fn test_get_progress_tracks_completion() {
client.complete_module(&admin, &learner, &course_id);
assert_eq!(client.get_progress(&learner, &course_id), 3);
}

// ── transfer_ownership ────────────────────────────────────────────────────────

#[test]
fn test_transfer_ownership_success() {
let (env, client) = setup();
let (_, instructor, id) = setup_with_course(&env, &client);
let new_instructor = Address::generate(&env);

client.transfer_ownership(&instructor, &new_instructor, &id);

let course = client.get_course(&id);
assert_eq!(course.instructor, new_instructor);
}

#[test]
fn test_transfer_ownership_emits_event() {
let (env, client) = setup();
let (_, instructor, id) = setup_with_course(&env, &client);
let new_instructor = Address::generate(&env);

client.transfer_ownership(&instructor, &new_instructor, &id);

// One OwnershipTransferred event must have been emitted
assert_eq!(env.events().all().len(), 1);
}

#[test]
#[should_panic(expected = "Unauthorized: Caller is not the course instructor")]
fn test_transfer_ownership_non_instructor_panics() {
let (env, client) = setup();
let (_, _, id) = setup_with_course(&env, &client);
let impostor = Address::generate(&env);
let new_instructor = Address::generate(&env);

client.transfer_ownership(&impostor, &new_instructor, &id);
}

#[test]
#[should_panic(expected = "Course not found")]
fn test_transfer_ownership_nonexistent_course_panics() {
let (env, client) = setup();
let admin = Address::generate(&env);
let instructor = Address::generate(&env);
let new_instructor = Address::generate(&env);

client.initialize(&admin);

client.transfer_ownership(&instructor, &new_instructor, &99);
}

#[test]
fn test_transfer_ownership_new_instructor_can_update_metadata() {
let (env, client) = setup();
let (_, instructor, id) = setup_with_course(&env, &client);
let new_instructor = Address::generate(&env);

client.transfer_ownership(&instructor, &new_instructor, &id);

// New instructor must be able to update metadata after ownership transfer
let updated_hash = BytesN::from_array(&env, &[9u8; 32]);
client.update_metadata(&id, &updated_hash);

let course = client.get_course(&id);
assert_eq!(course.metadata_hash, updated_hash);
}

#[test]
fn test_transfer_ownership_updates_instructor_field() {
let (env, client) = setup();
let (_, instructor, id) = setup_with_course(&env, &client);
let new_instructor = Address::generate(&env);

// Confirm original instructor before transfer
let before = client.get_course(&id);
assert_eq!(before.instructor, instructor);

client.transfer_ownership(&instructor, &new_instructor, &id);

// Confirm instructor field reflects the new address after transfer
let after = client.get_course(&id);
assert_eq!(after.instructor, new_instructor);
assert_ne!(after.instructor, instructor);
}
Loading
Loading