-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
factor out NIH link expire-time calculation into a standalone, testab…
…le method; add tests; refactor for correctness and Scala-ness
- Loading branch information
Showing
3 changed files
with
213 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
170 changes: 170 additions & 0 deletions
170
src/test/scala/org/broadinstitute/dsde/firecloud/service/ProfileClientSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
package org.broadinstitute.dsde.firecloud.service | ||
|
||
import org.broadinstitute.dsde.firecloud.model.Profile | ||
import org.broadinstitute.dsde.firecloud.core.ProfileClient | ||
import org.broadinstitute.dsde.firecloud.utils.DateUtils | ||
import org.scalatest.FreeSpec | ||
|
||
class ProfileClientSpec extends FreeSpec { | ||
|
||
|
||
def makeProfile(lastLinkTime: Option[Long] = None, linkExpireTime: Option[Long] = None): Profile = { | ||
Profile ( | ||
"firstName", | ||
"lastName", | ||
"title", | ||
"institute", | ||
"institutionalProgram", | ||
"programLocationCity", | ||
"programLocationState", | ||
"programLocationCountry", | ||
"pi", | ||
"nonProfitStatus", | ||
Some("billingAccountName"), | ||
Some("linkedNihUsername"), | ||
lastLinkTime, //lastLinkTime | ||
linkExpireTime, //linkExpireTime | ||
Some(false) //isDbGapAuthorized, unused | ||
) | ||
} | ||
|
||
def assertExpireTimeWasUpdated(calculatedExpire: Long) = { | ||
// we expect the calculated time to be now + 24 hours. We can't test this exactly because milliseconds | ||
// may have elapsed in processing, so we rely on DateUtils rounding, and give a | ||
val diff = DateUtils.hoursUntil(calculatedExpire) | ||
|
||
assert( diff == 23 || diff == 24, | ||
"Expected expiration 24 hours in the future, found expiration " + diff + " hours away" ) | ||
} | ||
|
||
|
||
"ExpireTimeCalculationTests" - { | ||
|
||
|
||
// following tests: lastLink MORE than 24 hours in the future | ||
"lastLink > 24 hours in the past and expire > 24 hours in the future" - { | ||
"should return expire as now+24hours" in { | ||
val lastLink = DateUtils.nowMinus30Days | ||
val expire = DateUtils.nowPlus30Days | ||
|
||
val profile = makeProfile(Some(lastLink), Some(expire)) | ||
val calculatedExpire = ProfileClient.calculateExpireTime(profile) | ||
|
||
assertExpireTimeWasUpdated(calculatedExpire) | ||
} | ||
} | ||
|
||
"lastLink > 24 hours in the past and expire < 24 hours in the future" - { | ||
"should return expire time unchanged" in { | ||
val lastLink = DateUtils.nowMinus30Days | ||
val expire = DateUtils.nowPlus1Hour | ||
|
||
val profile = makeProfile(Some(lastLink), Some(expire)) | ||
val calculatedExpire = ProfileClient.calculateExpireTime(profile) | ||
assertResult(expire) { calculatedExpire } | ||
} | ||
} | ||
|
||
"lastLink > 24 hours in the past and expire > 24 hours in the past" - { | ||
"should return expire time unchanged" in { | ||
val lastLink = DateUtils.nowMinus30Days | ||
val expire = DateUtils.nowMinus30Days | ||
|
||
val profile = makeProfile(Some(lastLink), Some(expire)) | ||
val calculatedExpire = ProfileClient.calculateExpireTime(profile) | ||
assertResult(expire) { calculatedExpire } | ||
} | ||
} | ||
|
||
"lastLink > 24 hours in the past and expire < 24 hours in the past" - { | ||
"should return expire time unchanged" in { | ||
val lastLink = DateUtils.nowMinus30Days | ||
val expire = DateUtils.nowMinus1Hour | ||
|
||
val profile = makeProfile(Some(lastLink), Some(expire)) | ||
val calculatedExpire = ProfileClient.calculateExpireTime(profile) | ||
assertResult(expire) { calculatedExpire } | ||
} | ||
} | ||
|
||
|
||
// following tests: lastLink LESS than 24 hours in the future | ||
"lastLink < 24 hours in the past and expire > 24 hours in the future" - { | ||
"should return expire time unchanged" in { | ||
val lastLink = DateUtils.nowMinus1Hour | ||
val expire = DateUtils.nowPlus30Days | ||
|
||
val profile = makeProfile(Some(lastLink), Some(expire)) | ||
val calculatedExpire = ProfileClient.calculateExpireTime(profile) | ||
assertResult(expire) { calculatedExpire } | ||
} | ||
} | ||
|
||
"lastLink < 24 hours in the past and expire < 24 hours in the future" - { | ||
"should return expire time unchanged" in { | ||
val lastLink = DateUtils.nowMinus1Hour | ||
val expire = DateUtils.nowPlus1Hour | ||
|
||
val profile = makeProfile(Some(lastLink), Some(expire)) | ||
val calculatedExpire = ProfileClient.calculateExpireTime(profile) | ||
assertResult(expire) { calculatedExpire } | ||
} | ||
} | ||
|
||
"lastLink < 24 hours in the past and expire > 24 hours in the past" - { | ||
"should return expire time unchanged" in { | ||
val lastLink = DateUtils.nowMinus1Hour | ||
val expire = DateUtils.nowMinus30Days | ||
|
||
val profile = makeProfile(Some(lastLink), Some(expire)) | ||
val calculatedExpire = ProfileClient.calculateExpireTime(profile) | ||
assertResult(expire) { calculatedExpire } | ||
} | ||
} | ||
|
||
"lastLink < 24 hours in the past and expire < 24 hours in the past" - { | ||
"should return expire time unchanged" in { | ||
val lastLink = DateUtils.nowMinus1Hour | ||
val expire = DateUtils.nowMinus1Hour | ||
|
||
val profile = makeProfile(Some(lastLink), Some(expire)) | ||
val calculatedExpire = ProfileClient.calculateExpireTime(profile) | ||
assertResult(expire) { calculatedExpire } | ||
} | ||
} | ||
|
||
"No lastLink or expire time" - { | ||
"should return expire as now+24hours" in { | ||
val profile = makeProfile() | ||
val calculatedExpire = ProfileClient.calculateExpireTime(profile) | ||
|
||
assertExpireTimeWasUpdated(calculatedExpire) | ||
} | ||
} | ||
|
||
"No lastLink time" - { | ||
"should return expire as now+24hours" in { | ||
val expire = DateUtils.nowPlus1Hour | ||
val profile = makeProfile(None, Some(expire)) | ||
val calculatedExpire = ProfileClient.calculateExpireTime(profile) | ||
|
||
assertExpireTimeWasUpdated(calculatedExpire) | ||
} | ||
} | ||
|
||
"No expire time" - { | ||
"should return expire as now+24hours" in { | ||
val lastLink = DateUtils.nowMinus1Hour | ||
val profile = makeProfile(Some(lastLink), None) | ||
val calculatedExpire = ProfileClient.calculateExpireTime(profile) | ||
|
||
assertExpireTimeWasUpdated(calculatedExpire) | ||
} | ||
} | ||
|
||
|
||
|
||
|
||
} | ||
|
||
} |