View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.core.provisioning.api.jexl;
20  
21  import static org.junit.jupiter.api.Assertions.assertFalse;
22  import static org.junit.jupiter.api.Assertions.assertNotNull;
23  import static org.junit.jupiter.api.Assertions.assertTrue;
24  
25  import java.io.IOException;
26  import java.io.StringWriter;
27  import java.util.ArrayList;
28  import java.util.HashMap;
29  import java.util.List;
30  import java.util.Map;
31  import java.util.UUID;
32  import org.apache.commons.jexl3.MapContext;
33  import org.apache.commons.lang3.SerializationUtils;
34  import org.apache.syncope.common.lib.Attr;
35  import org.apache.syncope.common.lib.to.MembershipTO;
36  import org.apache.syncope.common.lib.to.UserTO;
37  import org.apache.syncope.core.provisioning.api.AbstractTest;
38  import org.junit.jupiter.api.Test;
39  
40  public class MailTemplateTest extends AbstractTest {
41  
42      private static final String CONFIRM_PASSWORD_RESET_TEMPLATE =
43              "<html><body>"
44              + "<p>Hi,<br/> we are happy to inform you that the password request was successfully executed for "
45              + "your account.</p>  <p>Best regards.</p> </body> </html>";
46  
47      private static final String REQUEST_PASSWORD_RESET_TEMPLATE = "Hi, a password reset was requested for "
48              + "${user.getUsername()}.  In order to complete this request, you need to visit this link: "
49              + "http://localhost:9080/syncope-enduser/app/#!/confirmpasswordreset?token="
50              + "${input.get(0).replaceAll(' ', '%20')}"
51              + "If you did not request this reset, just ignore the present e-mail.  Best regards.";
52  
53      private static final String OPTIN_TEMPLATE =
54              "<html> <body> <h3>Hi ${user.getPlainAttr(\"firstname\").get().values[0]} "
55              + "${user.getPlainAttr(\"surname\").get().values[0]}, welcome to Syncope!</h3>"
56              + "<p> Your username is ${user.username}.<br/>"
57              + "Your email address is ${user.getPlainAttr(\"email\").get().values[0]}."
58              + "Your email address inside a <a href=\"http://localhost/?email="
59              + "${user.getPlainAttr(\"email\").get().values[0].replace('@', '%40')}\">link</a>.</p>"
60              + "<p>This message was sent to the following recipients: <ul>\n $$ for (recipient: recipients) {\n"
61              + "   <li>${recipient.getPlainAttr(\"email\").get().values[0]}</li>\n $$ }\n </ul>\n"
62              + "  because one of the following events occurred: <ul>\n $$ for (event: events) {\n"
63              + "   <li>${event}</li>\n $$ }\n </ul>\n </p> \n $$ if (!empty(user.memberships)) {\n"
64              + " You have been provided with the following groups:\n <ul>\n"
65              + " $$ for(membership : user.memberships) {\n   <li>${membership.groupName}</li>\n $$ }\n"
66              + " </ul>\n $$ }\n </body> </html>";
67  
68      private static String evaluate(final String template, final Map<String, Object> jexlVars) {
69          StringWriter writer = new StringWriter();
70          JexlUtils.newJxltEngine().
71                  createTemplate(template).
72                  evaluate(new MapContext(jexlVars), writer);
73          return writer.toString();
74      }
75  
76      @Test
77      public void confirmPasswordReset() throws IOException {
78          String htmlBody = evaluate(CONFIRM_PASSWORD_RESET_TEMPLATE, new HashMap<>());
79          assertNotNull(htmlBody);
80      }
81  
82      @Test
83      public void requestPasswordReset() throws IOException {
84          Map<String, Object> ctx = new HashMap<>();
85  
86          String username = "test" + UUID.randomUUID().toString();
87          UserTO user = new UserTO();
88          user.setUsername(username);
89          ctx.put("user", user);
90  
91          String token = "token " + UUID.randomUUID().toString();
92          List<String> input = new ArrayList<>();
93          input.add(token);
94          ctx.put("input", input);
95  
96          String textBody = evaluate(REQUEST_PASSWORD_RESET_TEMPLATE, ctx);
97  
98          assertNotNull(textBody);
99          assertTrue(textBody.contains("a password reset was requested for " + username + "."));
100         assertFalse(textBody.contains(
101                 "http://localhost:9080/syncope-enduser/app/#!/confirmpasswordreset?token="
102                 + token));
103         assertTrue(textBody.contains(
104                 "http://localhost:9080/syncope-enduser/app/#!/confirmpasswordreset?token="
105                 + token.replaceAll(" ", "%20")));
106     }
107 
108     @Test
109     public void optin() throws IOException {
110         Map<String, Object> ctx = new HashMap<>();
111 
112         String username = "test" + UUID.randomUUID().toString();
113         UserTO user = new UserTO();
114         user.setUsername(username);
115         user.getPlainAttrs().add(new Attr.Builder("firstname").value("John").build());
116         user.getPlainAttrs().add(new Attr.Builder("surname").value("Doe").build());
117         user.getPlainAttrs().add(new Attr.Builder("email").value("john.doe@syncope.apache.org").build());
118         user.getMemberships().add(new MembershipTO.Builder(UUID.randomUUID().toString()).groupName("a group").build());
119         ctx.put("user", user);
120 
121         String token = "token " + UUID.randomUUID().toString();
122         List<String> input = new ArrayList<>();
123         input.add(token);
124         ctx.put("input", input);
125 
126         UserTO recipient = SerializationUtils.clone(user);
127         recipient.getPlainAttr("email").get().getValues().set(0, "another@syncope.apache.org");
128         ctx.put("recipients", List.of(recipient));
129 
130         ctx.put("events", List.of("event1"));
131 
132         String htmlBody = evaluate(OPTIN_TEMPLATE, ctx);
133 
134         assertNotNull(htmlBody);
135 
136         assertTrue(htmlBody.contains("Hi John Doe,"));
137         assertTrue(htmlBody.contains("Your email address is john.doe@syncope.apache.org."));
138         assertTrue(htmlBody.contains("<li>another@syncope.apache.org</li>"));
139         assertTrue(htmlBody.contains("<li>a group</li>"));
140         assertTrue(htmlBody.contains("<li>event1</li>"));
141     }
142 }