|
| 1 | +package org.broadinstitute.dsde.firecloud.service |
| 2 | + |
| 3 | +import org.broadinstitute.dsde.firecloud.model.Profile |
| 4 | +import org.broadinstitute.dsde.firecloud.core.ProfileClient |
| 5 | +import org.broadinstitute.dsde.firecloud.utils.DateUtils |
| 6 | +import org.scalatest.FreeSpec |
| 7 | + |
| 8 | +class ProfileClientSpec extends FreeSpec { |
| 9 | + |
| 10 | + |
| 11 | + def makeProfile(lastLinkTime: Option[Long] = None, linkExpireTime: Option[Long] = None): Profile = { |
| 12 | + Profile ( |
| 13 | + "firstName", |
| 14 | + "lastName", |
| 15 | + "title", |
| 16 | + "institute", |
| 17 | + "institutionalProgram", |
| 18 | + "programLocationCity", |
| 19 | + "programLocationState", |
| 20 | + "programLocationCountry", |
| 21 | + "pi", |
| 22 | + "nonProfitStatus", |
| 23 | + Some("billingAccountName"), |
| 24 | + Some("linkedNihUsername"), |
| 25 | + lastLinkTime, //lastLinkTime |
| 26 | + linkExpireTime, //linkExpireTime |
| 27 | + Some(false) //isDbGapAuthorized, unused |
| 28 | + ) |
| 29 | + } |
| 30 | + |
| 31 | + def assertExpireTimeWasUpdated(calculatedExpire: Long) = { |
| 32 | + // we expect the calculated time to be now + 24 hours. We can't test this exactly because milliseconds |
| 33 | + // may have elapsed in processing, so we rely on DateUtils rounding, and give a |
| 34 | + val diff = DateUtils.hoursUntil(calculatedExpire) |
| 35 | + |
| 36 | + assert( diff == 23 || diff == 24, |
| 37 | + "Expected expiration 24 hours in the future, found expiration " + diff + " hours away" ) |
| 38 | + } |
| 39 | + |
| 40 | + |
| 41 | + "ExpireTimeCalculationTests" - { |
| 42 | + |
| 43 | + |
| 44 | + // following tests: lastLink MORE than 24 hours in the future |
| 45 | + "lastLink > 24 hours in the past and expire > 24 hours in the future" - { |
| 46 | + "should return expire as now+24hours" in { |
| 47 | + val lastLink = DateUtils.nowMinus30Days |
| 48 | + val expire = DateUtils.nowPlus30Days |
| 49 | + |
| 50 | + val profile = makeProfile(Some(lastLink), Some(expire)) |
| 51 | + val calculatedExpire = ProfileClient.calculateExpireTime(profile) |
| 52 | + |
| 53 | + assertExpireTimeWasUpdated(calculatedExpire) |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + "lastLink > 24 hours in the past and expire < 24 hours in the future" - { |
| 58 | + "should return expire time unchanged" in { |
| 59 | + val lastLink = DateUtils.nowMinus30Days |
| 60 | + val expire = DateUtils.nowPlus1Hour |
| 61 | + |
| 62 | + val profile = makeProfile(Some(lastLink), Some(expire)) |
| 63 | + val calculatedExpire = ProfileClient.calculateExpireTime(profile) |
| 64 | + assertResult(expire) { calculatedExpire } |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + "lastLink > 24 hours in the past and expire > 24 hours in the past" - { |
| 69 | + "should return expire time unchanged" in { |
| 70 | + val lastLink = DateUtils.nowMinus30Days |
| 71 | + val expire = DateUtils.nowMinus30Days |
| 72 | + |
| 73 | + val profile = makeProfile(Some(lastLink), Some(expire)) |
| 74 | + val calculatedExpire = ProfileClient.calculateExpireTime(profile) |
| 75 | + assertResult(expire) { calculatedExpire } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + "lastLink > 24 hours in the past and expire < 24 hours in the past" - { |
| 80 | + "should return expire time unchanged" in { |
| 81 | + val lastLink = DateUtils.nowMinus30Days |
| 82 | + val expire = DateUtils.nowMinus1Hour |
| 83 | + |
| 84 | + val profile = makeProfile(Some(lastLink), Some(expire)) |
| 85 | + val calculatedExpire = ProfileClient.calculateExpireTime(profile) |
| 86 | + assertResult(expire) { calculatedExpire } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + |
| 91 | + // following tests: lastLink LESS than 24 hours in the future |
| 92 | + "lastLink < 24 hours in the past and expire > 24 hours in the future" - { |
| 93 | + "should return expire time unchanged" in { |
| 94 | + val lastLink = DateUtils.nowMinus1Hour |
| 95 | + val expire = DateUtils.nowPlus30Days |
| 96 | + |
| 97 | + val profile = makeProfile(Some(lastLink), Some(expire)) |
| 98 | + val calculatedExpire = ProfileClient.calculateExpireTime(profile) |
| 99 | + assertResult(expire) { calculatedExpire } |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + "lastLink < 24 hours in the past and expire < 24 hours in the future" - { |
| 104 | + "should return expire time unchanged" in { |
| 105 | + val lastLink = DateUtils.nowMinus1Hour |
| 106 | + val expire = DateUtils.nowPlus1Hour |
| 107 | + |
| 108 | + val profile = makeProfile(Some(lastLink), Some(expire)) |
| 109 | + val calculatedExpire = ProfileClient.calculateExpireTime(profile) |
| 110 | + assertResult(expire) { calculatedExpire } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + "lastLink < 24 hours in the past and expire > 24 hours in the past" - { |
| 115 | + "should return expire time unchanged" in { |
| 116 | + val lastLink = DateUtils.nowMinus1Hour |
| 117 | + val expire = DateUtils.nowMinus30Days |
| 118 | + |
| 119 | + val profile = makeProfile(Some(lastLink), Some(expire)) |
| 120 | + val calculatedExpire = ProfileClient.calculateExpireTime(profile) |
| 121 | + assertResult(expire) { calculatedExpire } |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + "lastLink < 24 hours in the past and expire < 24 hours in the past" - { |
| 126 | + "should return expire time unchanged" in { |
| 127 | + val lastLink = DateUtils.nowMinus1Hour |
| 128 | + val expire = DateUtils.nowMinus1Hour |
| 129 | + |
| 130 | + val profile = makeProfile(Some(lastLink), Some(expire)) |
| 131 | + val calculatedExpire = ProfileClient.calculateExpireTime(profile) |
| 132 | + assertResult(expire) { calculatedExpire } |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + "No lastLink or expire time" - { |
| 137 | + "should return expire as now+24hours" in { |
| 138 | + val profile = makeProfile() |
| 139 | + val calculatedExpire = ProfileClient.calculateExpireTime(profile) |
| 140 | + |
| 141 | + assertExpireTimeWasUpdated(calculatedExpire) |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + "No lastLink time" - { |
| 146 | + "should return expire as now+24hours" in { |
| 147 | + val expire = DateUtils.nowPlus1Hour |
| 148 | + val profile = makeProfile(None, Some(expire)) |
| 149 | + val calculatedExpire = ProfileClient.calculateExpireTime(profile) |
| 150 | + |
| 151 | + assertExpireTimeWasUpdated(calculatedExpire) |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + "No expire time" - { |
| 156 | + "should return expire as now+24hours" in { |
| 157 | + val lastLink = DateUtils.nowMinus1Hour |
| 158 | + val profile = makeProfile(Some(lastLink), None) |
| 159 | + val calculatedExpire = ProfileClient.calculateExpireTime(profile) |
| 160 | + |
| 161 | + assertExpireTimeWasUpdated(calculatedExpire) |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + |
| 166 | + |
| 167 | + |
| 168 | + } |
| 169 | + |
| 170 | +} |
0 commit comments