Skip to content

#7784 If name is not set use 'Dataverse administrator' as email sender#7785

Merged
kcondon merged 6 commits intoIQSS:developfrom
pkiraly:7784-prevent-null-as-email-sender
Apr 26, 2021
Merged

#7784 If name is not set use 'Dataverse administrator' as email sender#7785
kcondon merged 6 commits intoIQSS:developfrom
pkiraly:7784-prevent-null-as-email-sender

Conversation

@pkiraly
Copy link
Member

@pkiraly pkiraly commented Apr 12, 2021

What this PR does / why we need it: If the :SystemEmail value does not contain a name, only an email address, and the user use contact form or the "Contact owner" form to send a mesage, the sender of the email will look like this:

null on behalf of name@example.com dataverse@example.edu

This change use the phrase "Dataverse administrator" instead of null if there is no name set.

Which issue(s) this PR closes: #7784

Closes #7784

Special notes for your reviewer: There is no integration test for this commit. I haven't found tests for sending emails. If you know one, please let me know and I will create a test for this. I also removed some empty lines and unwanted whitespaces from the file.

Suggestions on how to test this: I've created unit tests in src/test/java/edu/harvard/iq/dataverse/MailServiceBeanTest.java

Does this PR introduce a user interface change? If mockups are available, please link/include them here: No

Is there a release notes update needed for this change?: No

Additional documentation: No

throws UnsupportedEncodingException {
String personal = fromAddress.getPersonal();
if (personal == null)
personal = "Dataverse administrator";
Copy link
Contributor

@scolapasta scolapasta Apr 13, 2021

Choose a reason for hiding this comment

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

Not a full review, just yet, but can we put this in the bundle? (also let's call it as "Dataverse Installation Admin")

Copy link
Contributor

Choose a reason for hiding this comment

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

Actually, maybe a better alternative, rather than in a bundle, what if it used the branding name of the installation (using BrandingUtil.getInstallationBrandName())? Thoughts @qqmyers @djbrooke

Copy link
Contributor

Choose a reason for hiding this comment

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

Dataverse Installation Administrator is better than "null" so I'd be happy to accept a PR with that change, but if @pkiraly would be willing to add it, the branding name would be a better option.

Copy link
Member

Choose a reason for hiding this comment

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

That makes sense. There are also a getSupportTeamName()/getSupportTeamEmail() methods that might help -the former uses the brand name.

Copy link
Member Author

Choose a reason for hiding this comment

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

I made another version which solves it in a three step process:

        String personal = fromAddress.getPersonal() != null
            ? fromAddress.getPersonal()
            : BrandingUtil.getInstallationBrandName() != null
                ? BrandingUtil.getInstallationBrandName()
                : BundleUtil.getStringFromBundle("contact.delegation.default_personal");

In order to test it with unit tests I had to add some extra null check into getInstallationBrandName() method.

Copy link
Member

Choose a reason for hiding this comment

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

Sorry - I think there are two options. BrandingUtil requires some setup to work in tests (see the BrandingUtilTest class).

  1. Add a line like
    Mockito.when(dataverseSvc.getRootDataverseName()).thenReturn("LibraScholar");
    after line 62 resetting the return value to null, and probably to the MailUtilTest as well, and/or
  2. Add
    Mockito.when(settingsSvc.getValueForKey(SettingsServiceBean.Key.InstallationName)).thenReturn(null);
    //And configure the mock DataverseService to pretend the root collection name is as shown
    Mockito.when(dataverseSvc.getRootDataverseName()).thenReturn("LibraScholar");
    BrandingUtil.injectServices(dataverseSvc, settingsSvc);
    to your test to configure BrandingUtil with the values you want.

(Nominally, "LibraData" was the value that was returned prior to the recent updates to Dataverse to have BrandingUtil use services. With only BrandingUtilTest and MailUtilTest using BrandingUtil, I didn't worry about resetting for other tests. I guess that (1) would be cleaner, but doing (2) in your own tests should always work as well.)

Copy link
Member Author

@pkiraly pkiraly Apr 13, 2021

Choose a reason for hiding this comment

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

@qqmyers I tried both. (1) did not work at all, but (2) did with a modification. Mockito.when throws an UnnecessaryStubbingException. After some searching I found https://www.baeldung.com/mockito-unnecessary-stubbing-exception, which suggested to use Mockito.lenient().when and it worked. Now tests cover 4 cases:

  • the :SystemEmail contains a name part
  • if not, there is an :InstallationName
  • if not, the root Dataverse has a name
  • if not, there is a bundle property called contact.delegation.default_personal

Copy link
Member

Choose a reason for hiding this comment

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

@pkiraly - glad you got it to work. Thinking back, that stubbing exception may have been why I didn't do (1) myself. I can see why in (2), where you have the setup in a private method you could get the same exception - seems like Mockito is just checking whether the mock is used later in the same method - so I'm glad lenient works there. (Did you try lenient in (1)? Just curious - not suggesting that you need to modify the PR).

Copy link
Member Author

Choose a reason for hiding this comment

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

@qqmyers No, with (1) there were no effect at all, my impression was, that something did not happened at all. One more point: I wanted that these tests should work both alone and together with all the other tests. I guess when it was running alone it did not triggered BrandingUtilTest tests.

Copy link
Member

Choose a reason for hiding this comment

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

Hmm - I just tried (1) and get the unnecessary stubbing error - when the BrandingUtilTest runs. Using lenient() there gets rid of the warning, but I didn't test whether that would have any effect on other test classes (perhaps lenient() just eats the warning and doesn't actually reset the mock at all - which would be consistent if you see no effect of (1)).

In any case, I think what you have now works when testing one class or all of them and that's what needed.

Copy link
Contributor

@scolapasta scolapasta left a comment

Choose a reason for hiding this comment

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

One request; once resolved, I can approve this.

Comment on lines 19 to 25

String brandName = settingsService.getValueForKey(SettingsServiceBean.Key.InstallationName);
//Separate if statement simplifies test setup, otherwise could use the getValueForKey method with a default param
if(brandName==null) {

String brandName = null;
if (settingsService != null)
brandName = settingsService.getValueForKey(SettingsServiceBean.Key.InstallationName);
// Separate if statement simplifies test setup, otherwise could use the
// getValueForKey method with a default param
if (brandName == null && dataverseService != null) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Are these changes still needed now that you put initBrandingUtilWithRootDataverse() in each test? If not, please revert; if so, could we remove from her and do the necessary set up in the tests (for cleaner code here)?

Copy link
Member Author

Choose a reason for hiding this comment

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

@scolapasta You are right, I restored the original version (plus some small code formatting).

@kcondon kcondon self-assigned this Apr 22, 2021
@kcondon
Copy link
Contributor

kcondon commented Apr 22, 2021

@pkiraly Would you mind refreshing this branch from develop? The version has changed so I can't build and deploy it.

@pkiraly
Copy link
Member Author

pkiraly commented Apr 26, 2021

@kcondon
I updated the develop branch with

git checkout develop
git fetch upstream develop
git rebase upstream/develop
git push

then I updated branch 7784-prevent-null-as-email-sender which belongs to this PR:

git checkout 7784-prevent-null-as-email-sender
git merge develop
git push

Is that what you expected? If not, please let me know.

@kcondon
Copy link
Contributor

kcondon commented Apr 26, 2021

@pkiraly Thanks, testing now.

@kcondon kcondon merged commit 4571e38 into IQSS:develop Apr 26, 2021
@djbrooke djbrooke added this to the 5.5 milestone Apr 27, 2021
@pkiraly
Copy link
Member Author

pkiraly commented Jun 24, 2021

This PR was developed by Göttingen eResearch Alliance, Germany, and funded by SSHOC, "Social Sciences and Humanities Open Cloud". SSHOC has received funding from the European Union’s Horizon 2020 project call H2020-INFRAEOSC-04-2018, grant agreement #823782.

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.

Prevent "null" as email sender

5 participants