Пример #1
0
func TestParseJsonBody(t *testing.T) {
	var err error
	var body string
	var l *LTIStdHeaders

	template := `{
		"context_type": "%s",
		"lti_message_type": "%s",
		"lti_version": "%s",
		"roles":"%s,%s",
		"lis_person_contact_email_primary":"%s",
		"lis_person_name_full": "%s"
	}`

	/*
		Bad context type test
	*/
	body = fmt.Sprintf(template,
		//types.CtxTypeGroup,
		badCtxType,
		types.LTIBasicMessage,
		types.LTIVersion1_0,
		types.SysAdmin,
		types.InstAdmin,
		testPrimaryEmail,
		testFullName,
	)
	l, err = parseJsonBody(bytes.NewReader([]byte(body)))
	if err == nil {
		t.Errorf("Expected error: Bad context type should trigger error")
	}

	/*
		Bad lti message type test
	*/
	body = fmt.Sprintf(template,
		types.CtxTypeGroup,
		badLTIMessage,
		types.LTIVersion1_0,
		types.SysAdmin,
		types.InstAdmin,
		testPrimaryEmail,
		testFullName,
	)
	l, err = parseJsonBody(bytes.NewReader([]byte(body)))
	if err == nil {
		t.Errorf("Expected error: Bad message type should trigger error")
	}

	/*
		Bad lti version type test
	*/
	body = fmt.Sprintf(template,
		types.CtxTypeGroup,
		types.LTIBasicMessage,
		badLTIVersion,
		types.SysAdmin,
		types.InstAdmin,
		testPrimaryEmail,
		testFullName,
	)
	l, err = parseJsonBody(bytes.NewReader([]byte(body)))
	if err == nil {
		t.Errorf("Expected error: Bad version type should trigger error")
	}

	/*
		Parse valid body test
	*/
	body = fmt.Sprintf(template,
		types.CtxTypeGroup,
		types.LTIBasicMessage,
		types.LTIVersion1_0,
		types.SysAdmin,
		types.InstAdmin,
		testPrimaryEmail,
		testFullName,
	)
	l, err = parseJsonBody(bytes.NewReader([]byte(body)))
	if err != nil {
		t.Errorf("Unexpected error: body parse should have succeeded but got error '%s'", err)
	}
	if !l.ContextType.HasContextType(types.CtxTypeGroup) {
		t.Errorf("Bad marshal: Actual context type %s should be %s", l.ContextType, types.CtxTypeGroup)
	}
	if l.LTIMessageType != types.LTIMessage(types.LTIBasicMessage) {
		t.Errorf("Bad marshal: Actual message type %s should be %s", l.LTIMessageType, types.LTIBasicMessage)
	}
	if l.LTIVersion != types.LTIVersion(types.LTIVersion1_0) {
		t.Errorf("Bad marshal: Actual message type %s should be %s", l.LTIVersion, types.LTIVersion1_0)
	}
	if l.LISPersonPrimaryEmail != testPrimaryEmail {
		t.Errorf("Bad marshal: Actual primary email %s should be %s", l.LISPersonPrimaryEmail, testPrimaryEmail)
	}
	if l.LISPersonFullName != testFullName {
		t.Errorf("Bad marshal: Actual person full name %s should be %s", l.LISPersonFullName, testFullName)
	}
	if !l.Roles.HasSystemRole(types.SysAdmin) {
		t.Errorf("Bad marshal:  Of the present roles, %s, missing %s system role", l.Roles.GetSystemRoles(), types.SysAdmin)
	}
	if !l.Roles.HasInstitutionRole(types.InstAdmin) {
		t.Errorf("Bad marshal: Of the present roles, %s, missing %s institution role", l.Roles.GetInstitutionRoles(), types.InstAdmin)
	}
	if len(l.Roles.GetContextRoles()) != 0 {
		t.Errorf("Bad marshal: There should be no context roles present, but", l.Roles.GetContextRoles())
	}
	if len(l.Roles.GetUndefinedRoles()) != 0 {
		t.Errorf("Bad marshal: There should be no undefined roles present, but", l.Roles.GetUndefinedRoles())
	}
}
Пример #2
0
func TestParseUrlEncodedForm(t *testing.T) {
	var l *LTIStdHeaders
	var err error

	vals := url.Values{}

	vals.Add(ContextType, badCtxType)
	vals.Add(LISPersonPrimaryEmail, testPrimaryEmail)
	vals.Add(LISPersonFullName, testFullName)
	vals.Add(LTIMessageType, string(types.LTIBasicMessage))
	vals.Add(LTIVersion, string(types.LTIVersion1_0))
	vals.Add(Roles, fmt.Sprintf("%s,%s", types.SysAdmin, types.InstAdmin))

	/*
		Bad context type test
	*/
	l, err = parseUrlEncodedForm(vals)
	if err == nil {
		t.Logf("(IGNORING) Expected error: Bad context type should trigger error")
	}

	/*
		Bad lti message type test
	*/
	vals.Del(ContextType)
	vals.Add(ContextType, string(types.CtxTypeGroup))
	vals.Del(LTIMessageType)
	vals.Add(LTIMessageType, badLTIMessage)

	l, err = parseUrlEncodedForm(vals)
	if err == nil {
		t.Logf("(IGNORING) Expected error: Bad LTI message type should trigger error")
	}

	/*
		Bad lti version type test
	*/
	vals.Del(LTIMessageType)
	vals.Add(LTIMessageType, string(types.LTIBasicMessage))
	vals.Del(LTIVersion)
	vals.Add(LTIVersion, badLTIVersion)

	l, err = parseUrlEncodedForm(vals)
	if err == nil {
		t.Logf("(IGNORING) Expected error: Bad LTI version type should trigger error")
	}

	/*
		Parse valid body test
	*/
	vals.Del(LTIVersion)
	vals.Add(LTIVersion, string(types.LTIVersion1_0))

	l, err = parseUrlEncodedForm(vals)
	if err != nil {
		t.Errorf("Unexpected error: body parse should have succeeded but got error '%s'", err)
	}
	if !l.ContextType.HasContextType(types.CtxTypeGroup) {
		t.Errorf("Bad marshal: Actual context type %s should be %s", l.ContextType, types.CtxTypeGroup)
	}
	if l.LTIMessageType != types.LTIMessage(types.LTIBasicMessage) {
		t.Errorf("Bad marshal: Actual message type %s should be %s", l.LTIMessageType, types.LTIBasicMessage)
	}
	if l.LTIVersion != types.LTIVersion(types.LTIVersion1_0) {
		t.Errorf("Bad marshal: Actual message type %s should be %s", l.LTIVersion, types.LTIVersion1_0)
	}
	if l.LISPersonPrimaryEmail != testPrimaryEmail {
		t.Errorf("Bad marshal: Actual primary email %s should be %s", l.LISPersonPrimaryEmail, testPrimaryEmail)
	}
	if l.LISPersonFullName != testFullName {
		t.Errorf("Bad marshal: Actual person full name %s should be %s", l.LISPersonFullName, testFullName)
	}
	if !l.Roles.HasSystemRole(types.SysAdmin) {
		t.Errorf("Bad marshal:  Of the present roles, %s, missing %s system role", l.Roles.GetSystemRoles(), types.SysAdmin)
	}
	if !l.Roles.HasInstitutionRole(types.InstAdmin) {
		t.Errorf("Bad marshal: Of the present roles, %s, missing %s institution role", l.Roles.GetInstitutionRoles(), types.InstAdmin)
	}
	if len(l.Roles.GetContextRoles()) != 0 {
		t.Errorf("Bad marshal: There should be no context roles present, but", l.Roles.GetContextRoles())
	}
	if len(l.Roles.GetUndefinedRoles()) != 0 {
		t.Errorf("Bad marshal: There should be no undefined roles present, but", l.Roles.GetUndefinedRoles())
	}

	/*
		Ignore custom and ext headers for now
	*/
	vals.Add("custom_lineitems_url", "line_items")
	l, err = parseUrlEncodedForm(vals)
	if err != nil {
		t.Errorf("Unexpected error: body parse should have succeeded but got error '%s'", err)
	}
}