Browse files

Merge pull request #20 from trexart/master

Added tags to taglib to check for connected and disconnected state
  • Loading branch information...
2 parents 8fb3440 + a9003d8 commit f5739f719c18bd18ce04da5ea0cc24668f7cb6ca @antony committed Mar 11, 2013
View
38 grails-app/taglib/uk/co/desirableobjects/oauth/scribe/OauthTagLib.groovy
@@ -1,11 +1,14 @@
package uk.co.desirableobjects.oauth.scribe
import org.codehaus.groovy.grails.web.taglib.exceptions.GrailsTagException
+import org.scribe.model.Token
class OauthTagLib {
static namespace = 'oauth'
+ OauthService oauthService
+
def connect = { attrs, body ->
String provider = attrs.provider
@@ -19,4 +22,39 @@ class OauthTagLib {
}
+ def connected = { attrs, body ->
+
+ String provider = attrs.provider
+
+ if (!provider) {
+ throw new GrailsTagException('No provider specified for <oauth:connected /> tag. Try <oauth:connected provider="your-provider-name" />')
+ }
+
+ String sessionKey = oauthService.findSessionKeyForAccessToken(provider)
+ Token oauthToken = session[sessionKey]
+
+ if (oauthToken) {
+ out << body()
+ }
+
+ }
+
+ def disconnected = { attrs, body ->
+
+ String provider = attrs.provider
+
+ if (!provider) {
+ throw new GrailsTagException('No provider specified for <oauth:disconnected /> tag. Try <oauth:disconnected provider="your-provider-name" />')
+ }
+
+ String sessionKey = oauthService.findSessionKeyForAccessToken(provider)
+ Token oauthToken = session[sessionKey]
+
+ if (!oauthToken) {
+
+ out << body()
+ }
+
+ }
+
}
View
108 test/unit/uk/co/desirableobjects/oauth/scribe/OauthTagLibSpec.groovy
@@ -38,6 +38,90 @@ class OauthTagLibSpec extends Specification {
}
+ def 'an oauth connected tag body is rendered when token in session'() {
+
+ given:
+
+ tagLib.oauthService = new OauthService()
+ tagLib.oauthService.findSessionKeyForAccessToken('twitter') >> { return 'twitter:oasAccessToken' }
+
+ and:
+
+ tagLib.session['twitter:oasAccessToken'] = new Token('a', 'b', 'c')
+
+ when:
+
+ def output = tagLib.connected([provider:'twitter'], { 'Connected content' } )
+
+ then:
+
+ output == 'Connected content'
+
+ }
+
+ def 'an oauth connected tag body is NOT rendered when token does not exist in session'() {
+
+ given:
+
+ tagLib.oauthService = new OauthService()
+ tagLib.oauthService.findSessionKeyForAccessToken('twitter') >> { return 'twitter:oasAccessToken' }
+
+ and:
+
+ tagLib.session['twitter:oasAccessToken'] = null
+
+ when:
+
+ def output = tagLib.connected([provider:'twitter'], { 'Connected content' } )
+
+ then:
+
+ output == ''
+
+ }
+
+ def 'an oauth disconnected tag body is NOT rendered when token in session'() {
+
+ given:
+
+ tagLib.oauthService = new OauthService()
+ tagLib.oauthService.findSessionKeyForAccessToken('twitter') >> { return 'twitter:oasAccessToken' }
+
+ and:
+
+ tagLib.session['twitter:oasAccessToken'] = new Token('a', 'b', 'c')
+
+ when:
+
+ def output = tagLib.disconnected([provider:'twitter'], { 'Disconnected content' } )
+
+ then:
+
+ output == ''
+
+ }
+
+ def 'an oauth disconnected tag body is rendered when token does not exist in session'() {
+
+ given:
+
+ tagLib.oauthService = new OauthService()
+ tagLib.oauthService.findSessionKeyForAccessToken('twitter') >> { return 'twitter:oasAccessToken' }
+
+ and:
+
+ tagLib.session['twitter:oasAccessToken'] = null
+
+ when:
+
+ def output = tagLib.disconnected([provider:'twitter'], { 'Disconnected content' } )
+
+ then:
+
+ output == 'Disconnected content'
+
+ }
+
def 'an oauth link tag fails if provider is not specified'() {
when:
@@ -50,6 +134,30 @@ class OauthTagLibSpec extends Specification {
}
+ def 'an oauth connected tag fails if provider is not specified'() {
+
+ when:
+
+ tagLib.connected([:], { 'Connected content' } )
+
+ then:
+
+ thrown GrailsTagException
+
+ }
+
+ def 'an oauth disconnected tag fails if provider is not specified'() {
+
+ when:
+
+ tagLib.disconnected([:], { 'Disconnected content' } )
+
+ then:
+
+ thrown GrailsTagException
+
+ }
+
private boolean asExpectations() {
assertions.each { actual, expected ->

0 comments on commit f5739f7

Please sign in to comment.