<commit_before>package com.softwaremill.codebrag.service.user

import com.softwaremill.codebrag.dao.{ObjectIdTestUtils, UserDAO}
import org.scalatest.{BeforeAndAfter, FlatSpec}
import org.scalatest.matchers.ShouldMatchers
import org.scalatest.mock.MockitoSugar
import org.mockito.Mockito._
import org.mockito.BDDMockito._
import com.softwaremill.codebrag.domain.{Authentication, User}
import com.softwaremill.codebrag.service.data.UserJson
import org.bson.types.ObjectId
import com.softwaremill.codebrag.service.events.MockEventBus
import com.softwaremill.codebrag.service.commits.CommitReviewTaskGeneratorActions

class UserPasswordAuthenticatorSpec extends FlatSpec with ShouldMatchers with MockitoSugar with BeforeAndAfter with MockEventBus {

  var userDAO: UserDAO = _
  var authenticator: Authenticator = _
  var userDAOMock: UserDAO = _
  var reviewTaskGeneratorMock: CommitReviewTaskGeneratorActions = _
  val fixtureLogin = "johndoe"
  val fixturePassword = "password"
  val fixtureUserId: ObjectId = ObjectIdTestUtils.oid(123)
  val fixtureUser = User(fixtureUserId, Authentication.basic(fixtureLogin, fixturePassword), "<NAME>", "<EMAIL>", "123abc", "http://gravatar.com")

  before {
    eventBus.clear()
    userDAOMock = mock[UserDAO]
    reviewTaskGeneratorMock = mock[CommitReviewTaskGeneratorActions]
    authenticator = new UserPasswordAuthenticator(userDAOMock, eventBus, reviewTaskGeneratorMock)
  }

  it should "call dao to authenticate user" in {
    // given
    given(userDAOMock.findByLoginOrEmail(fixtureLogin)).willReturn(Some(fixtureUser))
    // when
    authenticator.authenticate(fixtureLogin, fixturePassword)

    // then
    verify(userDAOMock) findByLoginOrEmail(fixtureLogin)
  }

  it should "wrap dao response in UserJson" in {
    // given
    given(userDAOMock.findByLoginOrEmail(fixtureLogin)).willReturn(Some(fixtureUser))

    // when
    val result: Option[UserJson] = authenticator.authenticate(fixtureLogin, fixturePassword)

    // then
    result should equal(Option(UserJson(fixtureUser)))
  }

  it should "return None when user not found" in {
    // given
    given(userDAOMock.findByLoginOrEmail(fixtureLogin)).willReturn(None)

    // when
    val userOpt = authenticator.authenticate(fixtureLogin, fixturePassword)

    // then
    userOpt should be (None)
  }

  it should "return user if user credentials match" in {
    // given
    given(userDAOMock.findByLoginOrEmail(fixtureLogin)).willReturn(Some(fixtureUser))

    // when
    val Some(userOpt) = authenticator.authenticate(fixtureLogin, fixturePassword)

    userOpt.fullName should be(fixtureUser.name)
    userOpt.email should be(fixtureUser.email)<commit_msg>Minor - naming change<commit_after>package com.softwaremill.codebrag.service.user

import com.softwaremill.codebrag.dao.{ObjectIdTestUtils, UserDAO}
import org.scalatest.{BeforeAndAfter, FlatSpec}
import org.scalatest.matchers.ShouldMatchers
import org.scalatest.mock.MockitoSugar
import org.mockito.Mockito._
import org.mockito.BDDMockito._
import com.softwaremill.codebrag.domain.{Authentication, User}
import com.softwaremill.codebrag.service.data.UserJson
import org.bson.types.ObjectId
import com.softwaremill.codebrag.service.events.MockEventBus
import com.softwaremill.codebrag.service.commits.CommitReviewTaskGeneratorActions

class UserPasswordAuthenticatorSpec extends FlatSpec with ShouldMatchers with MockitoSugar with BeforeAndAfter with MockEventBus {

  var userDAO: UserDAO = _
  var authenticator: Authenticator = _
  var userDAOMock: UserDAO = _
  var reviewTaskGeneratorMock: CommitReviewTaskGeneratorActions = _
  val fixtureLogin = "johndoe"
  val fixturePassword = "password"
  val fixtureUserId: ObjectId = ObjectIdTestUtils.oid(123)
  val fixtureUser = User(fixtureUserId, Authentication.basic(fixtureLogin, fixturePassword), "John Doe", "<EMAIL>", "<PASSWORD>", "http://gravatar.com")

  before {
    eventBus.clear()
    userDAOMock = mock[UserDAO]
    reviewTaskGeneratorMock = mock[CommitReviewTaskGeneratorActions]
    authenticator = new UserPasswordAuthenticator(userDAOMock, eventBus, reviewTaskGeneratorMock)
  }

  it should "call dao to authenticate user" in {
    // given
    given(userDAOMock.findByLoginOrEmail(fixtureLogin)).willReturn(Some(fixtureUser))
    // when
    authenticator.authenticate(fixtureLogin, fixturePassword)

    // then
    verify(userDAOMock) findByLoginOrEmail(fixtureLogin)
  }

  it should "wrap dao response in UserJson" in {
    // given
    given(userDAOMock.findByLoginOrEmail(fixtureLogin)).willReturn(Some(fixtureUser))

    // when
    val result: Option[UserJson] = authenticator.authenticate(fixtureLogin, fixturePassword)

    // then
    result should equal(Option(UserJson(fixtureUser)))
  }

  it should "return None when user not found" in {
    // given
    given(userDAOMock.findByLoginOrEmail(fixtureLogin)).willReturn(None)

    // when
    val userOpt = authenticator.authenticate(fixtureLogin, fixturePassword)

    // then
    userOpt should be (None)
  }

  it should "return user if user credentials match" in {
    // given
    given(userDAOMock.findByLoginOrEmail(fixtureLogin)).willReturn(Some(fixtureUser))

    // when
    val Some(user) = authenticator.authenticate(fixtureLogin, fixturePassword)

    user.fullName should be(fixtureUser.name)
    user.email should be(fixtureUser.email)
{'fraction_non_alphanumeric': 0.089382347452883, 'fraction_numerical': 0.0016794178018286995, 'mean_word_length': 4.955555555555556, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '146194', 'n_tokens_mistral': 1661, 'n_tokens_neox': 1609, 'n_words': 322}