xapi-rs 0.2.0

A conformant LRS implementation of xAPI 2.0.0
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
// SPDX-License-Identifier: GPL-3.0-or-later

mod utils;

use chrono::{DateTime, NaiveDate, NaiveTime, TimeZone, Utc};
use iri_string::types::IriStr;
use josekit::jws::{self, JwsHeader, RS256};
use openssl::{sha::Sha256, x509::X509};
use rocket::http::Status;
use serde_json::{Map, Value};
use std::str::FromStr;
use test_context::test_context;
use tracing_test::traced_test;
use utils::{
    BOUNDARY, CR_LF, MyTestContext, accept_json, authorization, boundary_delimiter_line,
    content_type, multipart, read_to_string, v2,
};
use uuid::{Uuid, uuid};
use xapi_data::{
    Account, Activity, ActivityDefinition, Actor, Agent, Attachment, Group, MyDuration,
    MyLanguageTag, MyTimestamp, MyVersion, SIGNATURE_CT, SIGNATURE_UT, Score, Statement,
    StatementObject, StatementRef, Verb, Vocabulary, XResult, adl_verb,
};
use xapi_rs::{MyError, config};

const ID1: Uuid = uuid!("fd41c918-b88b-4b20-a0a5-a4c32391aaa0");
const URL1: &str = "http://example.com/xapi/verbs#sent-a-statement";

const ID2: Uuid = uuid!("7ccd3322-e1a5-411a-a67d-6a735c76f119");
const URL2: &str = "http://adlnet.gov/expapi/verbs/attempted";

#[test]
fn test_simple_statement() -> Result<(), MyError> {
    // 1. ensure we can correctly deserialize...
    let from_json = read_to_string("statement-simple", true);

    let de_result = serde_json::from_str::<Statement>(&from_json);
    assert!(de_result.is_ok());
    let st = de_result.unwrap();

    // 2. ensure we got a Statement w/ the expected properties + values...
    // 2.1. check the ID...
    let id = st.id();
    assert!(id.is_some());
    assert_eq!(id.unwrap(), &ID1);

    // 2.2. check the Actor...
    // 2.2.1 check it's an Agent...
    let actor = st.actor();
    // let actor = st.actor()?;
    assert!(actor.is_agent());
    assert!(!actor.is_group());
    let agent_from_statement = actor.as_agent().unwrap();

    // 2.2.2.a. check it's the expected Agent...
    let an = agent_from_statement.name();
    assert!(an.is_some());
    assert_eq!(an.unwrap(), "Project Tin Can API");
    let email = agent_from_statement.mbox();
    assert!(email.is_some());
    // to_uri ensures `mailto:` scheme is included in the output...
    assert_eq!(email.as_ref().unwrap().to_uri(), "mailto:user@example.com");
    // ...to_string doesn't...
    assert_eq!(email.unwrap().to_string(), "user@example.com");
    assert!(agent_from_statement.mbox_sha1sum().is_none());
    assert!(agent_from_statement.account().is_none());
    assert!(agent_from_statement.openid().is_none());

    // alternatively...
    // 2.2.2.b. build an Agent and compare it to deserialized version
    let agent = Agent::builder()
        .with_object_type()
        .name("Project Tin Can API")?
        .mbox("user@example.com")?
        .build()?;
    assert_eq!(agent, agent_from_statement);

    // 2.3. check the Verb...
    // 2.3.a. check every field...
    let verb_from_statement = st.verb();
    assert_eq!(verb_from_statement.id(), URL1);

    let en = MyLanguageTag::from_str("en")?;
    let us = MyLanguageTag::from_str("en-US")?;
    let fr = MyLanguageTag::from_str("fr")?;

    assert!(verb_from_statement.display(&en).is_none());
    assert!(verb_from_statement.display(&us).is_some());
    assert_eq!(verb_from_statement.display(&us).unwrap(), "sent");

    // alternatively...
    // 2.3.b. build a Verb and compare it to deserialized copy...
    let verb = Verb::builder().id(URL1)?.display(&us, "sent")?.build()?;
    assert_eq!(&verb, verb_from_statement);

    // 2.4. check the Object...
    // 2.4.1. check it's an Activity...
    let object = st.object();
    assert!(object.is_activity());
    assert!(!object.is_sub_statement());
    assert!(!object.is_agent());
    assert!(!object.is_group());
    assert!(!object.is_statement_ref());

    // 2.4.2.a. check it's the expected Activity...
    let activity_from_statement = object.as_activity().unwrap();
    assert_eq!(
        activity_from_statement.id(),
        "http://example.com/xapi/activity/simplestatement"
    );

    assert!(activity_from_statement.definition().is_some());
    let definition_from_statement = activity_from_statement.definition().unwrap();

    assert!(definition_from_statement.name(&en).is_none());
    assert!(definition_from_statement.name(&us).is_some());
    assert_eq!(
        definition_from_statement.name(&us).unwrap(),
        "simple statement"
    );

    assert!(definition_from_statement.description(&fr).is_none());
    assert!(definition_from_statement.description(&us).is_some());
    assert_eq!(
        definition_from_statement.description(&us).unwrap().len(),
        159
    );

    assert!(definition_from_statement.type_().is_none());
    assert!(definition_from_statement.more_info().is_none());
    assert!(definition_from_statement.interaction_type().is_none());
    assert!(
        definition_from_statement
            .correct_responses_pattern()
            .is_none()
    );
    assert!(definition_from_statement.choices().is_none());
    assert!(definition_from_statement.scale().is_none());
    assert!(definition_from_statement.source().is_none());
    assert!(definition_from_statement.target().is_none());
    assert!(definition_from_statement.steps().is_none());

    // alternatively
    // 2.4.2.b. build an Activity and compare it w/ the deserialized one...
    let activity_definition = ActivityDefinition::builder()
        .name(&us, "simple statement")?
        .description(
            &us,
            r#"A simple Experience API statement. Note that the LRS 
does not need to have any prior information about the Actor (learner), the 
verb, or the Activity/object."#,
        )?
        .build()?;
    assert_eq!(&activity_definition, definition_from_statement);
    let activity = Activity::builder()
        .id("http://example.com/xapi/activity/simplestatement")?
        .definition(activity_definition)?
        .build()?;
    assert_eq!(activity, activity_from_statement);

    // 2.5. check the timestamp...
    assert!(st.timestamp().is_some());
    assert_eq!(
        st.timestamp().unwrap().to_rfc3339(),
        "2015-11-18T12:17:00+00:00"
    );
    let ts_result = DateTime::parse_from_rfc3339(&st.timestamp().unwrap().to_rfc3339());
    assert!(ts_result.is_ok());
    let ts = ts_result.unwrap().with_timezone(&Utc);
    let date = ts.date_naive();
    assert_eq!(date.to_string(), "2015-11-18");
    // also ensure absence of millis in input does not affect result
    let time = ts.time();
    assert_eq!(time.to_string(), "12:17:00");
    assert_eq!(time, NaiveTime::from_hms_milli_opt(12, 17, 0, 0).unwrap());

    // 2.6. finally, ensure no other fields were found...
    assert!(st.result().is_none());
    assert!(st.context().is_none());
    assert!(st.stored().is_none());
    assert!(st.authority().is_none());
    assert!(st.version().is_none());
    assert!(st.attachments().is_empty());

    // 3. ensure we can serialize to JSON...
    let se_result = serde_json::to_string(&st);
    assert!(se_result.is_ok());
    let to_json = se_result.unwrap();

    // 4. finally check we produce _equivalent_ JSON as the example...
    // i say equivalent b/c while the same properties + values are present
    // they may be in a difference order and format.
    //
    // i cannot reliably compare JSON strings.  instead i'll do...
    let mut raw: Map<String, Value> = serde_json::from_str(&from_json).unwrap();
    println!("raw = {:?}", raw);
    let mut cooked: Map<String, Value> = serde_json::from_str(&to_json).unwrap();
    println!("cooked = {:?}", cooked);
    // NOTE (rsn) 20241018 - xAPI mandates that a conformant LRS must output
    // timestamps w/ seconds showing 3 decimals; i.e. w/ milli-second precision.
    // this is not what the example is using :(
    let raw_ts = raw.remove("timestamp").unwrap();
    let cooked_ts = cooked.remove("timestamp").unwrap();
    assert_eq!(cooked, raw);
    // the timestamps should now be equal even when different in format
    let raw_dt = DateTime::parse_from_str(
        &raw_ts.as_str().as_ref().unwrap().trim_matches('"'),
        "%Y-%m-%dT%H:%M:%S%:z",
    );
    assert!(raw_dt.is_ok());
    let raw_dt = raw_dt.unwrap().naive_utc();
    let cooked_dt = MyTimestamp::from_str(cooked_ts.as_str().unwrap());
    assert!(cooked_dt.is_ok());
    let cooked_dt = cooked_dt.unwrap().inner().naive_utc();
    assert_eq!(raw_dt, cooked_dt);

    Ok(())
}

#[traced_test]
#[test]
fn test_statement_w_attempted() -> Result<(), MyError> {
    let json = read_to_string("statement-simpleCBT", true);

    let de_result = serde_json::from_str::<Statement>(&json);
    assert!(de_result.is_ok());
    let st = de_result.unwrap();

    let id = st.id();
    assert!(id.is_some());
    assert_eq!(id.unwrap(), &ID2);

    // let actor = st.actor()?;
    let actor = st.actor();
    assert!(actor.is_agent());
    assert!(!actor.is_group());
    let agent = actor.as_agent().unwrap();
    let an = agent.name();
    assert!(an.is_some());
    assert_eq!(an.unwrap(), "Example Learner");
    let email = agent.mbox();
    assert!(email.is_some());
    assert_eq!(
        email.as_ref().unwrap().to_string(),
        "example.learner@adlnet.gov"
    );
    assert_eq!(email.unwrap().to_uri(), "mailto:example.learner@adlnet.gov");
    assert!(agent.mbox_sha1sum().is_none());
    assert!(agent.account().is_none());
    assert!(agent.openid().is_none());
    // alternatively...
    let agent = Agent::builder()
        .with_object_type()
        .name("Example Learner")?
        .mbox("example.learner@adlnet.gov")?
        .build()?;
    // assert_eq!(agent, st.actor()?.as_agent()?);
    assert_eq!(agent, st.actor().as_agent()?);

    let en = MyLanguageTag::from_str("en")?;
    let us = MyLanguageTag::from_str("en-US")?;
    let fr = MyLanguageTag::from_str("fr")?;

    let verb = st.verb();
    assert_eq!(verb.id(), URL2);
    assert!(verb.display(&us).is_some());
    assert_eq!(verb.display(&us).unwrap(), "attempted");
    // alternatively...
    let verb = Verb::builder()
        .id(URL2)?
        .display(&us, "attempted")?
        .build()?;

    assert_eq!(&verb, st.verb());
    // ...also b/c a Verb's display does not impact its meaning...
    let attempted = adl_verb(Vocabulary::Attempted);
    assert_ne!(&verb, attempted);
    assert!(verb.equivalent(attempted));

    let object = st.object();
    assert!(object.is_activity());
    assert!(!object.is_sub_statement());
    assert!(!object.is_agent());
    assert!(!object.is_group());
    assert!(!object.is_statement_ref());

    // check object (an activity) properties...
    let activity = object.as_activity().unwrap();
    assert_eq!(
        activity.id(),
        "http://example.adlnet.gov/xapi/example/simpleCBT"
    );

    assert!(activity.definition().is_some());
    let definition = activity.definition().unwrap();

    assert!(definition.name(&en).is_none());
    assert!(definition.name(&us).is_some());
    assert_eq!(definition.name(&us).unwrap(), "simple CBT course");

    assert!(definition.description(&fr).is_none());
    assert!(definition.description(&us).is_some());
    assert_eq!(
        definition.description(&us).unwrap(),
        "A fictitious example CBT course."
    );

    assert!(definition.type_().is_none());
    assert!(definition.more_info().is_none());
    assert!(definition.interaction_type().is_none());
    assert!(definition.correct_responses_pattern().is_none());
    assert!(definition.choices().is_none());
    assert!(definition.scale().is_none());
    assert!(definition.source().is_none());
    assert!(definition.target().is_none());
    assert!(definition.steps().is_none());

    assert!(st.timestamp().is_some());
    assert_eq!(
        st.timestamp().unwrap().to_rfc3339(),
        "2015-12-18T12:17:00+00:00"
    );
    // alternatively...
    let ts_result = DateTime::parse_from_rfc3339(&st.timestamp().unwrap().to_rfc3339());
    assert!(ts_result.is_ok());
    let ts = ts_result.unwrap().with_timezone(&Utc);
    let date = ts.date_naive();
    let time = ts.time();
    assert_eq!(date.to_string(), "2015-12-18");
    assert_eq!(time.to_string(), "12:17:00");
    // alternatively...
    let timestamp = Utc.with_ymd_and_hms(2015, 12, 18, 12, 17, 00).unwrap();
    assert_eq!(st.timestamp().unwrap(), &timestamp);

    assert!(st.result().is_some());
    let result = st.result().unwrap();

    assert!(result.score().is_some());
    let score = result.score().unwrap();
    assert!(score.scaled().is_some());
    assert_eq!(score.scaled().unwrap(), 0.95);
    assert!(score.raw().is_none());
    assert!(score.min().is_none());
    assert!(score.max().is_none());

    assert!(result.success().is_some());
    assert!(result.completion().unwrap());

    assert!(result.completion().is_some());
    assert!(result.completion().unwrap());

    assert!(result.duration().is_some());
    let duration = MyDuration::new(true, 0, 1234, 0).unwrap();
    assert_eq!(result.duration().unwrap(), &duration);

    assert!(result.response().is_none());
    assert!(result.extensions().is_none());

    assert!(st.context().is_none());
    assert!(st.stored().is_none());
    assert!(st.authority().is_none());
    assert!(st.version().is_none());
    assert!(st.attachments().is_empty());

    // so we're able to deserialize and get back the expected result.
    // can we construct an equivalent copy?
    // start w/ the statement's uuid but use a non-hyphenated version...
    let uuid = uuid!("7ccd3322e1a5411aa67d6a735c76f119");
    let agent = Agent::builder()
        .name("Example Learner")?
        .mbox("example.learner@adlnet.gov")?
        .build()?;
    tracing::debug!("agent = {}", agent);
    let verb = Verb::builder()
        .id("http://adlnet.gov/expapi/verbs/attempted")?
        .display(&us, "")?
        .build()?;
    tracing::debug!("verb = {}", verb);
    let definition = ActivityDefinition::builder()
        .name(&us, "simple CBT course")?
        .description(&us, "A fictitious example CBT course.")?
        .build()?;
    tracing::debug!("definition = {}", definition);
    let activity = Activity::builder()
        .id("http://example.adlnet.gov/xapi/example/simpleCBT")?
        .definition(definition)?
        .build()?;
    tracing::debug!("activity = {}", activity);
    let score = Score::builder().scaled(0.95)?.build()?;
    tracing::debug!("score = {}", score);
    let result = XResult::builder()
        .score(score)?
        .success(true)
        .completion(true)
        .duration("PT1234S")?
        .build()?;
    tracing::debug!("result = {}", result);

    let statement = Statement::builder()
        .id(&uuid.to_string())?
        .actor(Actor::Agent(agent))?
        .verb(verb)?
        .object(StatementObject::Activity(activity))?
        .result(result)?
        .timestamp("2015-12-18T12:17:00+00:00")?
        .build()?;
    tracing::debug!("statement = {}", statement);

    assert_ne!(statement, st);
    assert!(statement.equivalent(&st));

    Ok(())
}

#[traced_test]
#[test]
fn test_long_statement() -> Result<(), MyError> {
    let json = read_to_string("statement-long", true);

    let de_result = serde_json::from_str::<Statement>(&json);
    assert!(de_result.is_ok());
    let st = de_result.unwrap();

    // check actor...
    {
        // let actor = st.actor()?;
        let actor = st.actor();
        assert!(actor.is_group());
        assert!(!actor.is_agent());

        let group_from_statement = actor.as_group().unwrap();
        assert_eq!(group_from_statement.members().len(), 3);
        // deeper...
        let larry = Agent::builder()
            .name("Andrew Downes")?
            .account(
                Account::builder()
                    .home_page("http://www.example.com")?
                    .name("13936749")?
                    .build()?,
            )?
            .build()?;
        let curly = Agent::builder()
            .name("Toby Nichols")?
            .openid("http://toby.openid.example.org/")?
            .build()?;
        let moe = Agent::builder()
            .name("Ena Hills")?
            .mbox_sha1sum("ebd31e95054c018b10727ccffd2ef2ec3a016ee9")?
            .build()?;
        let group = Group::builder()
            .name("Team PB")?
            .mbox("teampb@example.com")?
            .member(moe)?
            .member(curly)?
            .member(larry)?
            .build()?;
        // equality v/s equivalence
        // assert_ne!(group_from_statement, group);
        assert!(group_from_statement.equivalent(&group));
    }

    // check object
    {
        let object = st.object();
        assert!(object.is_activity());
        assert!(!object.is_sub_statement());
        assert!(!object.is_agent());
        assert!(!object.is_group());
        assert!(!object.is_statement_ref());

        // check object (an activity) properties...
        let activity = object.as_activity().unwrap();
        assert_eq!(
            activity.id(),
            "http://www.example.com/meetings/occurances/34534"
        );

        assert!(activity.definition().is_some());
        let definition = activity.definition().unwrap();

        let en = MyLanguageTag::from_str("en")?;
        let us = MyLanguageTag::from_str("en-US")?;
        let gb = MyLanguageTag::from_str("en-GB")?;
        let fr = MyLanguageTag::from_str("fr")?;

        assert!(definition.name(&en).is_none());
        assert!(definition.name(&us).is_some());
        assert_eq!(definition.name(&us).unwrap(), "example meeting");

        assert!(definition.description(&fr).is_none());
        assert!(definition.description(&gb).is_some());
        assert_eq!(
            definition.description(&gb).unwrap(),
            "An example meeting that happened on a specific occasion with certain people present."
        );

        assert!(definition.type_().is_some());
        assert_eq!(
            definition.type_().unwrap(),
            "http://adlnet.gov/expapi/activities/meeting"
        );
        assert!(definition.more_info().is_some());
        assert_eq!(
            definition.more_info().unwrap(),
            "http://virtualmeeting.example.com/345256"
        );
        assert!(definition.interaction_type().is_none());
        assert!(definition.correct_responses_pattern().is_none());
        assert!(definition.choices().is_none());
        assert!(definition.scale().is_none());
        assert!(definition.source().is_none());
        assert!(definition.target().is_none());
        assert!(definition.steps().is_none());
        assert!(definition.extensions().is_some());
        assert_eq!(definition.extensions().unwrap().len(), 1);
        let iri =
            IriStr::new("http://example.com/profiles/meetings/activitydefinitionextensions/room")
                .expect("Failed parsing IRI");
        let ext = definition.extension(iri);
        assert!(ext.is_some());
        let actual_ext = serde_json::from_str::<Value>(
            r#"{"name": "Kilby", "id" : "http://example.com/rooms/342"}"#,
        );
        assert_eq!(ext.unwrap(), &actual_ext.unwrap());
    }

    // check timestamps...
    {
        assert!(st.timestamp().is_some());
        assert!(st.stored().is_some());
        assert_eq!(st.timestamp().unwrap(), st.stored().unwrap());
        let timestamp = NaiveDate::from_ymd_opt(2013, 5, 18)
            .unwrap()
            .and_hms_nano_opt(5, 32, 34, 804_000_000)
            .unwrap()
            .and_local_timezone(Utc)
            .unwrap();
        assert_eq!(st.timestamp().unwrap(), &timestamp);
    }

    // check authority...
    {
        assert!(st.authority().is_some());
        let authority = Agent::builder()
            .with_object_type()
            .account(
                Account::builder()
                    .home_page("http://cloud.scorm.com/")?
                    .name("anonymous")?
                    .build()?,
            )?
            .build()?;
        assert_eq!(st.authority().unwrap(), &Actor::Agent(authority));
    }

    // check version...
    {
        let version_from_statement = st.version().unwrap();
        let version = MyVersion::from_str("1.0.0").unwrap();
        assert_eq!(version_from_statement, &version);
    }

    // check result...
    {
        assert!(st.result().is_some());
        let result = st.result().unwrap();
        assert_eq!(result.success().unwrap(), true);
        assert_eq!(result.completion().unwrap(), true);
        assert_eq!(
            result.response().unwrap(),
            "We agreed on some example actions."
        );
        // let duration = Duration::new(true, 0, 60 * 60, 0).unwrap();
        let duration = MyDuration::new(true, 0, 60 * 60, 0).unwrap();
        assert_eq!(result.duration().unwrap(), &duration);
    }

    // check context...
    {
        assert!(st.context().is_some());
        let ctx = st.context().unwrap();
        assert_eq!(
            ctx.registration().unwrap(),
            &uuid!("ec531277b57b4c158d91d292c5b2b8f7")
        );

        assert!(ctx.instructor().is_some());
        let instructor = Agent::builder()
            .with_object_type()
            .name("Andrew Downes")?
            .account(
                Account::builder()
                    .home_page("http://www.example.com")?
                    .name("13936749")?
                    .build()?,
            )?
            .build()?;
        assert_eq!(ctx.instructor().unwrap(), &Actor::Agent(instructor));

        assert!(ctx.team().is_some());
        let team = Group::builder()
            .name("Team PB")?
            .mbox("teampb@example.com")?
            .build()?;
        assert_eq!(ctx.team().unwrap(), &team);

        assert!(ctx.platform().is_some());
        assert_eq!(ctx.platform().unwrap(), "Example virtual meeting software");
        assert!(ctx.language().is_some());
        assert_eq!(ctx.language().unwrap(), "tlh");
        assert!(ctx.statement().is_some());
        // the way we build UUID fields does not care about input case...
        assert_eq!(
            ctx.statement().unwrap(),
            &StatementRef::builder()
                .id("6690E6C93EF04ed38B377F3964730BEE")?
                .build()?
        );

        assert!(ctx.context_activities().is_some());
        assert!(ctx.context_agents().is_none());
        assert!(ctx.context_groups().is_none());

        // check context-activities...
        let ca = ctx.context_activities().unwrap();
        let p1 = Activity::builder()
            .with_object_type() // include `objectType`
            .id("http://www.example.com/meetings/series/267")?
            .build()?;
        assert_eq!(ca.parent(), [p1]);

        let en = MyLanguageTag::from_str("en")?;

        let c1 = Activity::builder()
            .with_object_type() // include `objectType`
            .id("http://www.example.com/meetings/categories/teammeeting")?
            .definition(
                ActivityDefinition::builder()
                    .name(&en, "team meeting")?
                    .description(&en, "A category of meeting used for regular team meetings.")?
                    .type_("http://example.com/expapi/activities/meetingcategory")?
                    .build()?,
            )?
            .build()?;
        assert_eq!(ca.category(), &[c1]);

        let o1 = Activity::builder()
            .with_object_type()
            .id("http://www.example.com/meetings/occurances/34257")?
            .build()?;
        let o2 = Activity::builder()
            .with_object_type()
            .id("http://www.example.com/meetings/occurances/3425567")?
            .build()?;
        assert_eq!(ca.other(), &[o1, o2]);
    }

    Ok(())
}

/// IMPORTANT (rsn) 20250213
/// =========================
/// This test uses the contents of the 'jws.sig' sample file taken from
/// [examples here][1]. May be the data was valid at the time it was first
/// published but it's not anymore. For one, the X.509 certificates have
/// expired. Their 'not_after' date is...
///   Mon Apr 03 2023 01:25:53 GMT+1000 (Australian Eastern Standard Time)
/// This means that it will always fail after the fix to Issue #8 if when
/// running the tests, the JWS_STRICT environment variable is TRUE.
///
/// [1]: https://opensource.ieee.org/xapi/xapi-base-standard-examples/-/blob/main/9274.1.1%20xAPI%20Base%20Standard%20Examples.md?ref_type=heads
///
#[test_context(MyTestContext)]
#[traced_test]
#[test]
fn test_signed_statement(ctx: &mut MyTestContext) -> Result<(), MyError> {
    // assemble signature attachment for the signed Statement from 'examples'.
    fn att_signature(sig: &str) -> Vec<u8> {
        let mut result = vec![];

        result.extend_from_slice(b"Content-Type: application/octet-stream\r\n");
        result.extend_from_slice(b"Content-Transfer-Encoding: binary\r\n");
        result.extend_from_slice(b"X-Experience-API-Hash: 672fa5fa658017f1b72d65036f13379c6ab05d4ab3b6664908d8acf0b6a0c634\r\n");
        result.extend_from_slice(CR_LF);
        result.extend_from_slice(sig.as_bytes());

        result
    }

    let client = &ctx.client;

    // POST a Statement w/ 1 Attachment and no additional parts...
    let (header, delimiter) = boundary_delimiter_line(BOUNDARY);
    let stmt = read_to_string("statement-signed", true);
    let sig = read_to_string("jws.sig", false);
    let body = multipart(&delimiter, &stmt, Some(att_signature(&sig)), None);
    let req = client
        .post("/statements")
        .body(body)
        .header(content_type(&header))
        .header(accept_json())
        .header(v2())
        .header(authorization());

    let resp = req.dispatch();
    let expected = if config().jws_strict {
        Status::BadRequest
    } else {
        Status::Ok
    };
    assert_eq!(resp.status(), expected);

    Ok(())
}

/// This test is supposed to exercise the JWS signature handling process when
/// JWS_STRICT environment variable is TRUE. Essentially we reproduce the same
/// type of artifacts the previous test uses but this time from valid data
/// which should lead to a successful outcome.
#[test_context(MyTestContext)]
#[traced_test]
#[test]
fn test_strict_signed_statement(ctx: &mut MyTestContext) -> Result<(), MyError> {
    // start by reading the correct and valid...
    // 1. JWS Signer certificate signed by the next one, containing the signer's
    //    RSA Public Key which will be used to verify the JWS Signature.
    // 2. self-signed certificate to emulate a Certificate Authority (CA) signing
    //    the above certificate.
    let c_str = read_to_string("C2.pem", false);
    let c = X509::from_pem(&c_str.as_bytes())?;
    let c_der = c.to_der()?;

    let ca_str = read_to_string("C1.pem", false);
    let ca = X509::from_pem(&ca_str.as_bytes())?;
    let ca_der = ca.to_der()?;

    // assemble the pair in a certificate chain array.  the serialize_compact()
    // method will take care of base-64 encoding them...
    let mut x5c = vec![];
    x5c.push(c_der);
    x5c.push(ca_der);

    let mut header = JwsHeader::new();
    header.set_algorithm("RS256");
    header.set_x509_certificate_chain(&x5c);

    // next, the payload; i.e. a Statement w/o signature attachment...
    let payload = read_to_string("statement-to-sign", true);

    // finally sign and output compact serialized form which will become the
    // contents of the attachment...
    let signer_private_key = read_to_string("P2_private.pem", false);
    let signer = RS256.signer_from_pem(&signer_private_key)?;
    let compact_sig = jws::serialize_compact(payload.as_bytes(), &header, &signer)?;

    // construct a content-type header...
    let ct_hdr = format!("Content-Type: {}\r\n", SIGNATURE_CT);
    // compute the SHA2 hash of the JWS compact serialized form...
    let mut hasher = Sha256::new();
    hasher.update(compact_sig.as_bytes());
    let hash = hex::encode(hasher.finish());
    let hash_hdr = format!("X-Experience-API-Hash: {}\r\n", hash);

    // assemble the signature attachment octets...
    let mut att_bytes = vec![];
    att_bytes.extend_from_slice(ct_hdr.as_bytes());
    att_bytes.extend_from_slice(b"Content-Transfer-Encoding: binary\r\n");
    att_bytes.extend_from_slice(hash_hdr.as_bytes());
    att_bytes.extend_from_slice(CR_LF);
    att_bytes.extend_from_slice(compact_sig.as_bytes());

    // construct an Attachment instance representing that signature...
    hasher = Sha256::new();
    hasher.update(compact_sig.as_bytes());
    let digest = hex::encode(hasher.finish());
    let signature_att = Attachment::builder()
        .usage_type(SIGNATURE_UT)?
        .content_type(SIGNATURE_CT)?
        .sha2(&digest)?
        .length(
            compact_sig
                .len()
                .try_into()
                .expect("Failed coercing to i64"),
        )?
        .build()?;

    // add it to the Statement we used as payload...
    let mut stmt = Statement::from_str(&payload)?;
    let old_atts = stmt.attachments();
    let new_atts = [old_atts, &[signature_att]].concat();
    stmt.set_attachments(new_atts);

    // JSON serialize it so we can post it...
    let stmt_json = serde_json::to_string(&stmt).expect("Failed serializing modified Statement");

    let client = &ctx.client;

    let (header, delimiter) = boundary_delimiter_line(BOUNDARY);
    let body = multipart(&delimiter, &stmt_json, Some(att_bytes), None);
    let req = client
        .post("/statements")
        .body(body)
        .header(content_type(&header))
        .header(accept_json())
        .header(v2())
        .header(authorization());

    let resp = req.dispatch();
    assert_eq!(resp.status(), Status::Ok);

    Ok(())
}