Skip to content

Commit

Permalink
Fail when pushbullet credentials are not valid
Browse files Browse the repository at this point in the history
Do not try to authenticate indefinitely when provided access token is not valid.
https://github.com/square/okhttp/wiki/Recipes#handling-authentication

Fixes #6
  • Loading branch information
jcgay committed Feb 12, 2017
1 parent 2b0bf87 commit a62638f
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 4 deletions.
11 changes: 11 additions & 0 deletions send-notification/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@
<artifactId>thumbnailator</artifactId>
<version>0.4.8</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
Expand Down Expand Up @@ -97,6 +102,12 @@
<version>1.16.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock</artifactId>
<version>1.58</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fr.jcgay.notification.notifier.anybar;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.io.Closeables;
import fr.jcgay.notification.Icon;
import fr.jcgay.notification.IconFileWriter;
import fr.jcgay.notification.SendNotificationException;
Expand Down Expand Up @@ -45,7 +46,9 @@ public void write(Icon icon) {
throw new SendNotificationException("Can't write notification icon: " + resizedIcon.getPath(), e);
} finally {
closeQuietly(input);
closeQuietly(output);
try {
Closeables.close(output, true);
} catch (IOException ignored) {}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,18 @@ public class PushbulletNotifier implements DiscoverableNotifier {
private static final Logger LOGGER = LoggerFactory.getLogger(PushbulletNotifier.class);

private final PushbulletConfiguration configuration;
private final String url;

private OkHttpClient client;

public PushbulletNotifier(Application application, PushbulletConfiguration configuration) {
this(application, configuration, "https://api.pushbullet.com/v2/pushes");
}

PushbulletNotifier(Application application, PushbulletConfiguration configuration, String url) {
LOGGER.debug("Configuring Pushbullet for application {}: {}.", application, configuration);
this.configuration = configuration;
this.url = url;
}

@Override
Expand All @@ -43,6 +49,9 @@ public Notifier init() {
@Override
public Request authenticate(Proxy proxy, Response response) throws IOException {
String credentials = Credentials.basic(configuration.key(), "");
if (credentials.equals(response.request().header("Authorization"))) {
return null; // If we already failed with these credentials, don't retry.
}
return response.request().newBuilder().header("Authorization", credentials).build();
}

Expand All @@ -58,7 +67,7 @@ public Request authenticateProxy(Proxy proxy, Response response) throws IOExcept
@Override
public void send(Notification notification) {
Request request = new Request.Builder()
.url("https://api.pushbullet.com/v2/pushes")
.url(url)
.post(buildRequestBody(notification))
.build();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fr.jcgay.notification.notifier.snarl;

import com.google.common.base.Objects;
import com.google.common.io.Closeables;
import fr.jcgay.notification.Application;
import fr.jcgay.notification.DiscoverableNotifier;
import fr.jcgay.notification.Notification;
Expand All @@ -13,7 +14,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static com.google.common.io.Closeables.closeQuietly;
import java.io.IOException;

public class SnarlNotifier implements DiscoverableNotifier {

Expand Down Expand Up @@ -71,7 +72,9 @@ public void send(Notification notification) {
@Override
public void close() {
if (snarl != null) {
closeQuietly(snarl);
try {
Closeables.close(snarl, true);
} catch (IOException ignored) {}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package fr.jcgay.notification.notifier.pushbullet

import com.github.tomakehurst.wiremock.junit.WireMockRule
import fr.jcgay.notification.Application
import fr.jcgay.notification.Icon
import fr.jcgay.notification.Notification
import org.junit.Rule
import spock.lang.Specification

import static com.github.tomakehurst.wiremock.client.WireMock.*
import static fr.jcgay.notification.Notification.Level.ERROR

class PushbulletNotifierSpec extends Specification {

@Rule
WireMockRule wireMock = new WireMockRule()

def "should fail when credentials are not valid"() {
given:
def icon = Icon.create(new URL('file:/icon.png'), 'icon')
def application = Application.builder('id', 'name', icon).build()
Properties configuration = [
'notifier.pushbullet.apikey':'access-token-not-valid',
]

and:
def pushbullet = new PushbulletNotifier(application, PushbulletConfiguration.create(configuration), "http://localhost:${wireMock.port()}/pushes")
pushbullet.init()

and:
wireMock.stubFor(post(urlEqualTo("/pushes")).willReturn(aResponse().withStatus(401)))

when:
pushbullet.send(Notification.builder('title', 'message', icon)
.subtitle('subtitle')
.level(ERROR)
.build())

then:
def result = thrown(PushbulletNotificationException)
result.message == 'Pushbullet notification has failed, [401] - [Unauthorized]\n\nCheck your configuration at: https://github.com/jcgay/send-notification/wiki/Pushbullet'
}
}

0 comments on commit a62638f

Please sign in to comment.