Skip to content

Commit d8a7c53

Browse files
RajeshGovosanjuktaghosh7Manvendra-P-Singh
authored
Unit Test Case for gmail_update_signature (googleworkspace#190)
* git-on-borg files of gmail-api-snippets * Update build.gradle test12 * Update build.gradle * Unit Test Case for gmail_update_signature Co-authored-by: sanjuktaghosh7 <sanjuktaghosh@google.com> Co-authored-by: Manvendra-P-Singh <singhmanvendra@google.com>
1 parent 861b132 commit d8a7c53

2 files changed

Lines changed: 42 additions & 8 deletions

File tree

gmail/snippets/src/main/java/UpdateSignature.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2021 Google LLC
1+
// Copyright 2022 Google LLC
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -32,13 +32,19 @@ public class UpdateSignature {
3232
/**
3333
* Update the gmail signature.
3434
*
35-
* @throws IOException
35+
* @return the updated signature id
36+
* @throws IOException - if service account credentials file not found.
3637
*/
37-
public static void updateGmailSignature() throws IOException {
38-
// Load pre-authorized user credentials from the environment.
39-
// TODO(developer) - See https://developers.google.com/identity for
40-
// guides on implementing OAuth2 for your application.
41-
GoogleCredentials credentials = GoogleCredentials.getApplicationDefault().createScoped(Collections.singleton(GmailScopes.GMAIL_SETTINGS_BASIC));
38+
public static String updateGmailSignature() throws IOException {
39+
// TODO(developer) - Replace with your email address.
40+
String USER_EMAIL_ADDRESS = "gduser1@workspacesamples.dev";
41+
42+
/* Load pre-authorized user credentials from the environment.
43+
TODO(developer) - See https://developers.google.com/identity for
44+
guides on implementing OAuth2 for your application. */
45+
GoogleCredentials credentials = GoogleCredentials.getApplicationDefault()
46+
.createScoped(Collections.singleton(GmailScopes.GMAIL_SETTINGS_BASIC))
47+
.createDelegated(USER_EMAIL_ADDRESS);
4248
HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(
4349
credentials);
4450

@@ -59,14 +65,15 @@ public static void updateGmailSignature() throws IOException {
5965
}
6066
}
6167
// Updating a new signature
62-
SendAs aliasSettings = new SendAs().setSignature("I heart cats.");
68+
SendAs aliasSettings = new SendAs().setSignature("Automated Signature");
6369
SendAs result = service.users().settings().sendAs().patch(
6470
"me",
6571
primaryAlias.getSendAsEmail(),
6672
aliasSettings)
6773
.execute();
6874
//Prints the updated signature
6975
System.out.println("Updated signature - " + result.getSignature());
76+
return result.getSignature();
7077
} catch (GoogleJsonResponseException e) {
7178
// TODO(developer) - handle error appropriately
7279
System.err.println("Unable to update signature: " + e.getDetails());
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
16+
import org.junit.Test;
17+
import java.io.IOException;
18+
import static org.junit.Assert.assertEquals;
19+
20+
public class TestUpdateSignature extends BaseTest{
21+
22+
@Test
23+
public void testUpdateGmailSignature() throws IOException{
24+
String signature = UpdateSignature.updateGmailSignature();
25+
assertEquals("Automated Signature", signature);
26+
}
27+
}

0 commit comments

Comments
 (0)