Skip to content

Comments

AP-580: Add validations for CalNet user attributes #22

Merged
yzhoubk merged 19 commits intomainfrom
AP-580
Feb 24, 2026
Merged

AP-580: Add validations for CalNet user attributes #22
yzhoubk merged 19 commits intomainfrom
AP-580

Conversation

@yzhoubk
Copy link
Contributor

@yzhoubk yzhoubk commented Feb 13, 2026

During Rails upgrade code review testing, we discovered users were missing email addresses. Investigation showed that a CalNet schema attribute had been renamed, though we were not notified when the change occurred. This ticket aims to address unexpected attribute changes from CalNet and improve our handling of schema updates.

Copy link
Member

@awilfox awilfox left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great and should help with debugging AP-583 as well. r+ after tiny question.

Copy link
Member

@awilfox awilfox left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, sorry, I didn't see the test failures. Looks like Rubocop has some suggestions, and we need to fix up the mock user objects in the other tests to have the required attributes. Still, I approve of the overall work - just need to fix those issues before merging.

Copy link
Member

@anarchivist anarchivist left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed with @awilfox - this is looking good, but let's address the test failures before moving forward.

@yzhoubk
Copy link
Contributor Author

yzhoubk commented Feb 19, 2026

@anarchivist, @awilfox

I added berkeleyEduAlternateId as a fallback attribute when retrieving the email from CalNet. I also moved the CalNet attribute name mappings into a config file so they can be shared by both the User model and CalNet-related tests, without modifying the CalNet data fixtures.

For the student ID value, since there is no berkeleyEduStuID attribute in the response, I temporarily fell back to using berkeleyEduCSID, as the fixture data and comments suggest that these two attributes contain the same value.

However, after further review, I think we should not introduce a fallback for the berkeleyEduStuID attribute. In my local CalNet response, the berkeleyEduStuID attribute is missing simply because I am not a student. Previously, the User model did not validate CalNet attributes, so we did not notice the absence of berkeleyEduStuID. - That's my guess.

If this is the case, a student might also not have an employeeNumber if he/she is not a student employee.

It seems we need a sample CalNet response for an actual student account to clarify the expected attributes. Based on that, we can decide how to properly update the current validation logic for studentID and employeeID, or we just eliminate to validate related attributes: berkeleyEduStuID and employeeNumber .

I will rebase locally to fix the conflict tomorrow.

@awilfox
Copy link
Member

awilfox commented Feb 19, 2026

@yzhoubk I agree that we should gather a real CalNet student response, anonymise it, and then use it as part of our testing. It would be beneficial to have both a student employee and a student non-employee to see how employeeNumber behaves.

The overall direction of this MR looks good. I will review it in-depth when you are ready.

@yzhoubk
Copy link
Contributor Author

yzhoubk commented Feb 21, 2026

The PR isn’t ready for review yet — I’m planning to refactor and restructure some of the code .

@yzhoubk
Copy link
Contributor Author

yzhoubk commented Feb 23, 2026

@awilfox It's ready for review. I've added implementation notes in slack, please take a look.

Copy link
Member

@awilfox awilfox left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few minor typos, but otherwise looks great. Going to test against Rails 7.2 in a bit.

# NOTE: berkeleyEduCSID should be same as berkeleyEduStuID for students
{
affiliations: get_attribute_from_auth(auth_extra, :affiliations),
cs_id: auth_extra['berkeleyEduCSID'], # Not included in CALNET_ATTRS because it's not used by any applications; Just keept it here.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
cs_id: auth_extra['berkeleyEduCSID'], # Not included in CALNET_ATTRS because it's not used by any applications; Just keept it here.
cs_id: auth_extra['berkeleyEduCSID'], # Not included in CALNET_ATTRS because it's not used by any applications; Just keep it here.

minor typo.

end

def raise_missing_calnet_attribute_error(auth_extra, missing)
missing_attrs = "Expected Calnet attribute(s) not found (case-sensitive): #{missing.join(', ')}."
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
missing_attrs = "Expected Calnet attribute(s) not found (case-sensitive): #{missing.join(', ')}."
missing_attrs = "Expected CalNet attribute(s) not found (case-sensitive): #{missing.join(', ')}."

actual = %w[berkeleyEduAffiliations berkeleyEduAlternatid berkeleyEduCSID berkeleyEduIsMemberOf berkeleyEduUCPathID departmentNumber
displayName employeeNumber givenName surname uid]
# rubocop:disable Layout/LineLength
msg = "Expected Calnet attribute(s) not found (case-sensitive): #{missing.join(', ')}. The actual CalNet attributes: #{actual.join(', ')}. The user is expected display name"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
msg = "Expected Calnet attribute(s) not found (case-sensitive): #{missing.join(', ')}. The actual CalNet attributes: #{actual.join(', ')}. The user is expected display name"
msg = "Expected CalNet attribute(s) not found (case-sensitive): #{missing.join(', ')}. The actual CalNet attributes: #{actual.join(', ')}. The user is expected display name"

when the other fix is applied, this will need to be as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for catching the typos!

@awilfox
Copy link
Member

awilfox commented Feb 23, 2026

Confirmed that this works on Rails 7.2 as well (applied atop #28).

@yzhoubk yzhoubk changed the title Draft: Validate email presence across codebase AP-580: Validate email presence across codebase Feb 24, 2026
@yzhoubk yzhoubk changed the title AP-580: Validate email presence across codebase AP-580: Add validations for CalNet user attributes Feb 24, 2026
@yzhoubk yzhoubk merged commit e78e784 into main Feb 24, 2026
5 checks passed
@yzhoubk yzhoubk deleted the AP-580 branch February 24, 2026 00:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants